-- Migration 0001 — initial schema for v0.1 learner state (D-007).
-- Creates learner, sessions, turns, progress tables + the hardcoded learner-1 row.

-- Schema (also in db/schema.sql for reference; this is the migration source).
CREATE TABLE IF NOT EXISTS learner (
    id           TEXT PRIMARY KEY,
    display_name TEXT NOT NULL,
    created_at   TEXT NOT NULL DEFAULT (datetime('now'))
);

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,
    outcome               TEXT,
    cost_estimated_cents  INTEGER,
    debrief_text          TEXT,
    cost_breakdown_json   TEXT
);

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,
    asr_text    TEXT,
    tts_text    TEXT,
    latency_ms  REAL,
    created_at  TEXT NOT NULL DEFAULT (datetime('now')),
    UNIQUE(session_id, seq)
);

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)
);

-- The single hardcoded learner row (D-007 — no auth in v0.1).
INSERT OR IGNORE INTO learner (id, display_name) VALUES ('learner-1', 'Alex');