-- 0001_init.sql — initial schema for Cerbero Bite (docs/05-data-model.md)
--
-- Forward-only. Run by state/migrations.py once user_version == 0.
-- Bumps user_version to 1 at the end.

PRAGMA foreign_keys = ON;

CREATE TABLE positions (
    proposal_id        TEXT PRIMARY KEY,
    spread_type        TEXT NOT NULL,
    asset              TEXT NOT NULL DEFAULT 'ETH',
    expiry             TEXT NOT NULL,
    short_strike       NUMERIC NOT NULL,
    long_strike        NUMERIC NOT NULL,
    short_instrument   TEXT NOT NULL,
    long_instrument    TEXT NOT NULL,
    n_contracts        INTEGER NOT NULL,
    spread_width_usd   NUMERIC NOT NULL,
    spread_width_pct   NUMERIC NOT NULL,
    credit_eth         NUMERIC NOT NULL,
    credit_usd         NUMERIC NOT NULL,
    max_loss_usd       NUMERIC NOT NULL,
    spot_at_entry      NUMERIC NOT NULL,
    dvol_at_entry      NUMERIC NOT NULL,
    delta_at_entry     NUMERIC NOT NULL,
    eth_price_at_entry NUMERIC NOT NULL,
    proposed_at        TEXT NOT NULL,
    opened_at          TEXT,
    closed_at          TEXT,
    close_reason       TEXT,
    debit_paid_eth     NUMERIC,
    pnl_eth            NUMERIC,
    pnl_usd            NUMERIC,
    status             TEXT NOT NULL,
    created_at         TEXT NOT NULL,
    updated_at         TEXT NOT NULL
);

CREATE INDEX idx_positions_status ON positions(status);
CREATE INDEX idx_positions_closed_at ON positions(closed_at);

CREATE TABLE instructions (
    instruction_id   TEXT PRIMARY KEY,
    proposal_id      TEXT NOT NULL REFERENCES positions(proposal_id),
    kind             TEXT NOT NULL,
    payload_json     TEXT NOT NULL,
    sent_at          TEXT NOT NULL,
    acknowledged_at  TEXT,
    filled_at        TEXT,
    cancelled_at     TEXT,
    actual_fill_eth  NUMERIC,
    actual_fees_eth  NUMERIC
);

CREATE INDEX idx_instructions_proposal ON instructions(proposal_id);

CREATE TABLE decisions (
    id            INTEGER PRIMARY KEY AUTOINCREMENT,
    decision_type TEXT NOT NULL,
    proposal_id   TEXT,
    timestamp     TEXT NOT NULL,
    inputs_json   TEXT NOT NULL,
    outputs_json  TEXT NOT NULL,
    action_taken  TEXT,
    notes         TEXT
);

CREATE INDEX idx_decisions_timestamp ON decisions(timestamp);
CREATE INDEX idx_decisions_proposal ON decisions(proposal_id);

CREATE TABLE dvol_history (
    timestamp TEXT PRIMARY KEY,
    dvol      NUMERIC NOT NULL,
    eth_spot  NUMERIC NOT NULL
);

CREATE TABLE manual_actions (
    id           INTEGER PRIMARY KEY AUTOINCREMENT,
    kind         TEXT NOT NULL,
    proposal_id  TEXT,
    payload_json TEXT,
    created_at   TEXT NOT NULL,
    consumed_at  TEXT,
    consumed_by  TEXT,
    result       TEXT
);

CREATE INDEX idx_manual_actions_unconsumed ON manual_actions(consumed_at);

CREATE TABLE system_state (
    id                INTEGER PRIMARY KEY CHECK (id = 1),
    kill_switch       INTEGER NOT NULL DEFAULT 0,
    kill_reason       TEXT,
    kill_at           TEXT,
    last_health_check TEXT NOT NULL,
    last_kelly_calib  TEXT,
    config_version    TEXT NOT NULL,
    started_at        TEXT NOT NULL
);

PRAGMA user_version = 1;
