-- Praxis v0.1 SQLite schema — learner state (D-007).
-- Single hardcoded learner, no auth, no multi-tenant.

-- The single learner row (D-007). v0.1 has one hardcoded profile.
CREATE TABLE IF NOT EXISTS learner (
    id           TEXT PRIMARY KEY,
    display_name TEXT NOT NULL,
    created_at   TEXT NOT NULL DEFAULT (datetime('now'))
);

-- Session log: one row per voice session.
CREATE TABLE IF NOT EXISTS sessions (
    id                    TEXT PRIMARY KEY,
    learner_id            TEXT NOT NULL REFERENCES learner(id),
    scenario_id           TEXT NOT NULL,
    started_at            TEXT NOT NULL DEFAULT (datetime('now')),
    ended_at              TEXT,
    branch_path_json      TEXT,          -- JSON array of branch ids taken
    outcome               TEXT,          -- 'success' | 'failure' | NULL
    cost_estimated_cents  INTEGER,       -- derived per-session cost (D-012)
    debrief_text          TEXT,          -- TASK-05-05: the generated debrief
    cost_breakdown_json   TEXT           -- TASK-04-04: token/minute/char breakdown
);

-- Turn log: one row per ASR/TTS turn within a session.
CREATE TABLE IF NOT EXISTS turns (
    id          INTEGER PRIMARY KEY AUTOINCREMENT,
    session_id  TEXT NOT NULL REFERENCES sessions(id),
    seq         INTEGER NOT NULL,
    role        TEXT NOT NULL,            -- 'user' | 'assistant'
    asr_text    TEXT,
    tts_text    TEXT,
    latency_ms  REAL,
    created_at  TEXT NOT NULL DEFAULT (datetime('now')),
    UNIQUE(session_id, seq)
);

-- Progress: per-learner per-scenario progression (v0.1: attempts + last outcome).
CREATE TABLE IF NOT EXISTS progress (
    learner_id     TEXT NOT NULL REFERENCES learner(id),
    scenario_id    TEXT NOT NULL,
    attempts       INTEGER NOT NULL DEFAULT 0,
    last_outcome   TEXT,
    updated_at     TEXT NOT NULL DEFAULT (datetime('now')),
    PRIMARY KEY (learner_id, scenario_id)
);