-- ~/.hermes/profiles/cto-planb/schema.sql (Hermes-native install location)
-- cto-planb runtime state — work queue, sandcastle invocation log, agent runtime.

CREATE TABLE IF NOT EXISTS work_queue (
    id            TEXT PRIMARY KEY,                       -- uuid
    title         TEXT NOT NULL,
    status        TEXT NOT NULL DEFAULT 'queued',         -- queued|sandboxing|reviewing|pr-open|blocked|done|killed
    target_repo   TEXT NOT NULL,                          -- relative path or URL
    brief         TEXT,                                   -- JSON delegation brief from CEO/JP
    prompt_file   TEXT,                                   -- path to sandcastle prompt
    sandbox       TEXT NOT NULL DEFAULT 'docker',         -- docker|podman|vercel|noSandbox
    branch        TEXT,                                   -- temp branch sandcastle creates
    pr_url        TEXT,                                   -- github PR URL when opened
    verdict       TEXT,                                   -- accept|re-sandcastle|escalate
    iteration     INTEGER NOT NULL DEFAULT 0,             -- sandcastle re-run count
    max_iterations INTEGER NOT NULL DEFAULT 3,            -- escalate after this
    notes         TEXT,                                   -- judgment notes
    created_at    TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at    TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS invocations (
    id            INTEGER PRIMARY KEY AUTOINCREMENT,
    work_id       TEXT NOT NULL,                          -- FK work_queue.id
    iteration     INTEGER NOT NULL,
    sandbox       TEXT NOT NULL,                          -- docker|podman|...
    started_at    TEXT NOT NULL DEFAULT (datetime('now')),
    ended_at      TEXT,
    exit_code     INTEGER,
    commits       TEXT,                                   -- JSON array of {sha}
    completion    TEXT,                                   -- matched completion signal if any
    error         TEXT,
    FOREIGN KEY (work_id) REFERENCES work_queue(id)
);

CREATE TABLE IF NOT EXISTS agent_runtime (
    key           TEXT PRIMARY KEY,
    value         TEXT
);

-- Seed agent_runtime defaults
INSERT OR IGNORE INTO agent_runtime (key, value) VALUES
    ('status', 'sleeping'),                              -- sleeping|running|halted
    ('version', '0.1.0'),
    ('last_invocation_at', NULL);
