-- version: 21
-- SP39: resubmissions audit table for tracking corrected-file SFTP pushes.
--
-- One row per claim per push. Status (pending_999 / 999_accepted /
-- 999_rejected / 277ca_accepted / paid / denied_again) is derived at
-- read-time by joining against claim_acks (via the existing SP28/31
-- auto-link) + remittances (via CLP->claim). No denormalized status
-- column on this row — the existing auto-link data is the source of
-- truth and we want to avoid a write-coordination problem between
-- the SFTP push and the inbound ack ingestion.
--
-- Idempotency on (claim_id, interchange_control_number): the resubmit
-- CLI can be re-run safely without producing duplicate rows for the
-- same push.

CREATE TABLE resubmissions (
    id                          INTEGER PRIMARY KEY AUTOINCREMENT,
    claim_id                    TEXT NOT NULL,
    batch_id                    TEXT NOT NULL,
    resubmitted_at              DATETIME NOT NULL,
    source_corrected_path       TEXT NOT NULL,
    interchange_control_number  TEXT NOT NULL,
    group_control_number        TEXT NOT NULL
);

CREATE INDEX ix_resubmissions_claim_id ON resubmissions(claim_id);
CREATE INDEX ix_resubmissions_batch_id ON resubmissions(batch_id);

CREATE UNIQUE INDEX ux_resubmissions_claim_icn
    ON resubmissions(claim_id, interchange_control_number);