-- claw-core initial schema
-- Creates the four core tables used by the claw-core store modules.

CREATE TABLE IF NOT EXISTS active_memory (
    id         TEXT    NOT NULL,
    agent_id   TEXT    NOT NULL,
    key        TEXT    NOT NULL,
    value      TEXT    NOT NULL,
    updated_at TEXT    NOT NULL,
    expires_at TEXT,
    PRIMARY KEY (id),
    UNIQUE (agent_id, key)
);

CREATE TABLE IF NOT EXISTS session_state (
    id         TEXT NOT NULL,
    session_id TEXT NOT NULL,
    key        TEXT NOT NULL,
    value      TEXT NOT NULL,
    updated_at TEXT NOT NULL,
    PRIMARY KEY (id),
    UNIQUE (session_id, key)
);

CREATE TABLE IF NOT EXISTS tool_output (
    id         TEXT    NOT NULL PRIMARY KEY,
    session_id TEXT    NOT NULL,
    tool_name  TEXT    NOT NULL,
    output     TEXT    NOT NULL,
    success    INTEGER NOT NULL DEFAULT 1,
    created_at TEXT    NOT NULL
);

CREATE TABLE IF NOT EXISTS context (
    id         TEXT NOT NULL PRIMARY KEY,
    session_id TEXT NOT NULL,
    key        TEXT NOT NULL,
    value      TEXT NOT NULL,
    created_at TEXT NOT NULL,
    expires_at TEXT NOT NULL
);
