-- Chat Switchboard — 015 Tool Health (SQLite)

CREATE TABLE IF NOT EXISTS tool_health (
    id              TEXT PRIMARY KEY,
    tool_name       TEXT NOT NULL,
    window_start    TEXT NOT NULL,
    request_count   INTEGER NOT NULL DEFAULT 0,
    error_count     INTEGER NOT NULL DEFAULT 0,
    total_latency_ms INTEGER NOT NULL DEFAULT 0,
    max_latency_ms  INTEGER NOT NULL DEFAULT 0,
    last_error      TEXT,
    last_error_at   TEXT,
    updated_at      TEXT NOT NULL DEFAULT (datetime('now')),
    UNIQUE (tool_name, window_start)
);

CREATE INDEX IF NOT EXISTS idx_tool_health_window ON tool_health(tool_name, window_start);
