-- version: 5
-- TA1 (Interchange Acknowledgment) table.
--
-- Mirrors the `acks` table for the lower-level envelope ack. A TA1 file
-- is the per-interchange acknowledgment that payers send back in response
-- to an 837 submission; a 999 acks individual transaction sets inside the
-- interchange. Both are persisted side-by-side.
--
-- Schema follows the same conventions as `acks`:
--   * `id` auto-increment PK (one row per parsed TA1 file)
--   * `source_batch_id` FK to a synthetic `batches.id` (`TA1-<ISA13>`) so
--     the row has a stable identifier even though there's no inbound 837
--     batch to point at
--   * flat columns for the TA1 fields so list endpoints can sort/filter
--     without parsing `raw_json`
--   * `raw_json` carries the full `ParseResultTa1` for the detail endpoint

CREATE TABLE ta1_acks (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    source_batch_id TEXT NOT NULL REFERENCES batches(id) ON DELETE CASCADE,
    control_number TEXT NOT NULL,
    interchange_date DATE,
    interchange_time TEXT,
    ack_code TEXT NOT NULL,
    note_code TEXT,
    ack_generated_date DATE,
    sender_id TEXT NOT NULL DEFAULT '',
    receiver_id TEXT NOT NULL DEFAULT '',
    parsed_at DATETIME NOT NULL,
    raw_json TEXT
);

CREATE INDEX ix_ta1_acks_source_batch_id ON ta1_acks(source_batch_id);
CREATE INDEX ix_ta1_acks_ack_code ON ta1_acks(ack_code);