-- version: 7
-- SP9: multi-payer, multi-NPI, SFTP stub
-- See spec: docs/superpowers/specs/2026-06-20-cyclone-multi-payer-npi-sftp-design.md

CREATE TABLE providers (
    npi              TEXT PRIMARY KEY,
    label            TEXT NOT NULL,
    legal_name       TEXT NOT NULL,
    tax_id           TEXT NOT NULL,
    taxonomy_code    TEXT NOT NULL,
    address_line1    TEXT NOT NULL,
    address_line2    TEXT,
    city             TEXT NOT NULL,
    state            TEXT NOT NULL,
    zip              TEXT NOT NULL,
    is_active        INTEGER NOT NULL DEFAULT 1,
    created_at       TEXT NOT NULL,
    updated_at       TEXT NOT NULL
);

CREATE TABLE payers (
    payer_id         TEXT PRIMARY KEY,
    name             TEXT NOT NULL,
    receiver_name    TEXT NOT NULL,
    receiver_id      TEXT NOT NULL,
    is_active        INTEGER NOT NULL DEFAULT 1,
    created_at       TEXT NOT NULL,
    updated_at       TEXT NOT NULL
);

CREATE TABLE payer_configs (
    payer_id         TEXT NOT NULL REFERENCES payers(payer_id),
    transaction_type TEXT NOT NULL,
    config_json      TEXT NOT NULL,
    updated_at       TEXT NOT NULL,
    PRIMARY KEY (payer_id, transaction_type)
);

CREATE TABLE clearhouse (
    id                       INTEGER PRIMARY KEY CHECK (id = 1),
    name                     TEXT NOT NULL,
    tpid                     TEXT NOT NULL,
    submitter_name           TEXT NOT NULL,
    submitter_id_qual        TEXT NOT NULL DEFAULT '46',
    submitter_contact_name   TEXT NOT NULL,
    submitter_contact_email  TEXT NOT NULL,
    filename_block_json      TEXT NOT NULL,
    sftp_block_json          TEXT NOT NULL,
    updated_at               TEXT NOT NULL
);

CREATE INDEX ix_providers_active ON providers(is_active);
CREATE INDEX ix_payers_active ON payers(is_active);
