-- call_monitor schema v1.0.0
-- Event-log de invocaciones del agente al registry + telemetria asociada.
-- Issue 0085. Aditivo. Aplicado via embed.FS al abrir operations.db.

PRAGMA journal_mode=WAL;
PRAGMA foreign_keys=ON;

-- Sesiones Claude Code. Una por arranque de claude-code.
CREATE TABLE IF NOT EXISTS sessions (
    session_id    TEXT PRIMARY KEY,
    cwd           TEXT NOT NULL DEFAULT '',
    started_at    INTEGER NOT NULL,
    ended_at      INTEGER,
    health_score  REAL,
    mcp_ratio     REAL,
    notes         TEXT NOT NULL DEFAULT ''
);

CREATE INDEX IF NOT EXISTS idx_sessions_started ON sessions(started_at);

-- Cada invocacion del agente sobre funcion del registry (heredoc/mcp/fn_run).
CREATE TABLE IF NOT EXISTS calls (
    id             INTEGER PRIMARY KEY AUTOINCREMENT,
    session_id     TEXT NOT NULL DEFAULT '',
    function_id    TEXT NOT NULL DEFAULT '',
    tool_used      TEXT NOT NULL,
    args_hash      TEXT NOT NULL DEFAULT '',
    duration_ms    INTEGER NOT NULL DEFAULT 0,
    success        INTEGER NOT NULL DEFAULT 1,
    error_class    TEXT NOT NULL DEFAULT '',
    error_snippet  TEXT NOT NULL DEFAULT '',
    ts             INTEGER NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_calls_function ON calls(function_id);
CREATE INDEX IF NOT EXISTS idx_calls_session  ON calls(session_id);
CREATE INDEX IF NOT EXISTS idx_calls_ts       ON calls(ts);
CREATE INDEX IF NOT EXISTS idx_calls_tool     ON calls(tool_used);
CREATE INDEX IF NOT EXISTS idx_calls_success  ON calls(success);

-- Edit/Write del agente sobre archivos del registry.
CREATE TABLE IF NOT EXISTS code_writes (
    id             INTEGER PRIMARY KEY AUTOINCREMENT,
    session_id     TEXT NOT NULL DEFAULT '',
    function_id    TEXT NOT NULL DEFAULT '',
    file_path      TEXT NOT NULL,
    lines_added    INTEGER NOT NULL DEFAULT 0,
    lines_removed  INTEGER NOT NULL DEFAULT 0,
    ts             INTEGER NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_code_writes_function ON code_writes(function_id);
CREATE INDEX IF NOT EXISTS idx_code_writes_ts       ON code_writes(ts);

-- Test run de unit tests del registry (go test / pytest / etc.).
CREATE TABLE IF NOT EXISTS test_runs (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    session_id      TEXT NOT NULL DEFAULT '',
    function_id     TEXT NOT NULL DEFAULT '',
    test_id         TEXT NOT NULL DEFAULT '',
    passed          INTEGER NOT NULL DEFAULT 1,
    duration_ms     INTEGER NOT NULL DEFAULT 0,
    output_snippet  TEXT NOT NULL DEFAULT '',
    ts              INTEGER NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_test_runs_function ON test_runs(function_id);
CREATE INDEX IF NOT EXISTS idx_test_runs_passed   ON test_runs(passed);
CREATE INDEX IF NOT EXISTS idx_test_runs_ts       ON test_runs(ts);

-- E2E checks de apps que dependen de una funcion del registry.
CREATE TABLE IF NOT EXISTS e2e_runs_fn (
    id           INTEGER PRIMARY KEY AUTOINCREMENT,
    session_id   TEXT NOT NULL DEFAULT '',
    function_id  TEXT NOT NULL,
    app_id       TEXT NOT NULL,
    check_id     TEXT NOT NULL,
    passed       INTEGER NOT NULL DEFAULT 1,
    ts           INTEGER NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_e2e_function ON e2e_runs_fn(function_id);
CREATE INDEX IF NOT EXISTS idx_e2e_app      ON e2e_runs_fn(app_id);
CREATE INDEX IF NOT EXISTS idx_e2e_passed   ON e2e_runs_fn(passed);

-- Antipatrones detectados (sqlite3 inline, import *, heredoc reescribiendo, etc.).
CREATE TABLE IF NOT EXISTS violations (
    id               INTEGER PRIMARY KEY AUTOINCREMENT,
    session_id       TEXT NOT NULL DEFAULT '',
    rule_id          TEXT NOT NULL,
    function_id      TEXT NOT NULL DEFAULT '',
    command_snippet  TEXT NOT NULL DEFAULT '',
    severity         TEXT NOT NULL DEFAULT 'warning',
    ts               INTEGER NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_violations_rule     ON violations(rule_id);
CREATE INDEX IF NOT EXISTS idx_violations_function ON violations(function_id);
CREATE INDEX IF NOT EXISTS idx_violations_severity ON violations(severity);

-- Heredocs/snippets clusterizados por similitud (alimenta proposals new_function).
CREATE TABLE IF NOT EXISTS patterns (
    pattern_hash           TEXT PRIMARY KEY,
    representative_snippet TEXT NOT NULL,
    occurrences            INTEGER NOT NULL DEFAULT 1,
    session_ids_json       TEXT NOT NULL DEFAULT '[]',
    first_seen             INTEGER NOT NULL,
    last_seen              INTEGER NOT NULL,
    proposal_id            TEXT NOT NULL DEFAULT ''
);

CREATE INDEX IF NOT EXISTS idx_patterns_occurrences ON patterns(occurrences);
CREATE INDEX IF NOT EXISTS idx_patterns_last_seen   ON patterns(last_seen);
