-- Schema SQLite (WAL) pentru gateway RAR AUTOPASS.
-- Vezi plan.md sect. 5. NICIUN camp pentru parole RAR.
-- Validarea completa (T3) si criptarea PII (P2) vin ulterior; in schelet
-- payload-ul e stocat ca JSON text (camp payload_json), de inlocuit cu BLOB
-- criptat + purge_after cand se face T7/criptare.

PRAGMA journal_mode = WAL;
PRAGMA foreign_keys = ON;

-- Conturi ROAAUTO (clientii care folosesc gateway-ul).
CREATE TABLE IF NOT EXISTS accounts (
    id          INTEGER PRIMARY KEY AUTOINCREMENT,
    name        TEXT NOT NULL,
    cui         TEXT,
    created_at  TEXT NOT NULL DEFAULT (datetime('now'))
);

-- Chei API per cont (separate de creds RAR). Stocam doar hash-ul.
CREATE TABLE IF NOT EXISTS api_keys (
    id          INTEGER PRIMARY KEY AUTOINCREMENT,
    account_id  INTEGER NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
    key_hash    TEXT NOT NULL UNIQUE,
    active      INTEGER NOT NULL DEFAULT 1,
    created_at  TEXT NOT NULL DEFAULT (datetime('now')),
    revoked_at  TEXT
);

-- Mapare operatie service -> codPrestatie RAR (← mapare_prestatii.DBF, T5).
CREATE TABLE IF NOT EXISTS operations_mapping (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    account_id      INTEGER NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
    cod_op_service  TEXT NOT NULL,
    cod_prestatie   TEXT NOT NULL,
    auto_send       INTEGER NOT NULL DEFAULT 1,
    created_at      TEXT NOT NULL DEFAULT (datetime('now')),
    UNIQUE (account_id, cod_op_service)
);

-- Cache nomenclator RAR {codPrestatie, numePrestatie} (← prestatii_rar.DBF / live).
CREATE TABLE IF NOT EXISTS nomenclator_rar (
    cod_prestatie   TEXT PRIMARY KEY,
    nume_prestatie  TEXT NOT NULL,
    updated_at      TEXT NOT NULL DEFAULT (datetime('now'))
);

-- Coada de prezentari catre RAR. Masina de stari: plan.md sect. 3.
CREATE TABLE IF NOT EXISTS submissions (
    id               INTEGER PRIMARY KEY AUTOINCREMENT,
    idempotency_key  TEXT NOT NULL UNIQUE,
    account_id       INTEGER REFERENCES accounts(id) ON DELETE SET NULL,
    status           TEXT NOT NULL DEFAULT 'queued'
                       CHECK (status IN ('queued','sending','sent','needs_mapping','needs_data','error')),
    payload_json     TEXT NOT NULL,          -- TODO(P2): inlocuit cu BLOB criptat
    rar_status_code  INTEGER,
    rar_error        TEXT,
    id_prezentare    INTEGER,                -- data.id intors de RAR la succes
    retry_count      INTEGER NOT NULL DEFAULT 0,
    sending_since    TEXT,                   -- pentru lease/timeout pe randuri 'sending' orfane (T2)
    purge_after      TEXT,                   -- sent + 90z (P2)
    created_at       TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at       TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_submissions_status ON submissions(status);

-- Heartbeat worker (un singur rand, id=1). /healthz citeste de aici.
CREATE TABLE IF NOT EXISTS worker_heartbeat (
    id                  INTEGER PRIMARY KEY CHECK (id = 1),
    last_beat           TEXT,
    last_rar_login_ok   TEXT,
    detail              TEXT
);
INSERT OR IGNORE INTO worker_heartbeat (id, last_beat, detail) VALUES (1, NULL, 'never started');
