-- version: 11
-- SP16: Inbound MFT polling scheduler
--
-- Tracks every file the background scheduler has downloaded from
-- the Gainwell MFT inbound path so a re-tick (or a restart) does not
-- re-process the same file. Idempotency is required for production:
-- the scheduler polls every N seconds and a slow MFT server may hand
-- us the same file across two polls.
--
-- We key on (sftp_block_name, name) — the sftp_block_name disambiguates
-- multi-provider installations (SP9+SP-multi-NPI), name is the inbound
-- filename as it appears on the MFT server.
--
-- Status values:
--   * ok       — parsed cleanly, results persisted to the store
--   * error    — parser raised; error_message captured for the operator
--   * skipped  — file_type not in the scheduler's allowed set
--   * pending  — file was downloaded but a downstream step failed
--                (e.g. DB write); the scheduler retries on the next tick
--
-- claim_count is the number of claims/remittances/acks the parser
-- surfaced. Surfaced on /api/admin/scheduler/status so the operator can
-- see throughput without parsing logs.
--
-- Compliance: not part of the HIPAA audit chain (SP11). This is
-- operational metadata; an SFTP outage shouldn't pollute the audit log.

CREATE TABLE processed_inbound_files (
    id                INTEGER PRIMARY KEY AUTOINCREMENT,
    sftp_block_name   TEXT NOT NULL,
    name              TEXT NOT NULL,
    size              INTEGER NOT NULL,
    modified_at       TEXT,
    file_type         TEXT,
    processed_at      TEXT NOT NULL,
    parser_used       TEXT,
    claim_count       INTEGER NOT NULL DEFAULT 0,
    status            TEXT NOT NULL,
    error_message     TEXT
);

CREATE UNIQUE INDEX ux_processed_inbound_files_block_name
    ON processed_inbound_files(sftp_block_name, name);
CREATE INDEX ix_processed_inbound_files_processed_at
    ON processed_inbound_files(processed_at DESC);
CREATE INDEX ix_processed_inbound_files_status
    ON processed_inbound_files(status);