-- Agent Context API tables
-- Migration: 010_agent_tables.sql

-- Registered/approved agents (identity binding)
CREATE TABLE IF NOT EXISTS agents (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    agent_id TEXT NOT NULL UNIQUE,  -- UUID from agent
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    last_seen DATETIME,
    trusted BOOLEAN DEFAULT 1       -- can be revoked
);

-- Pending access requests and active sessions
CREATE TABLE IF NOT EXISTS agent_sessions (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    request_token TEXT NOT NULL UNIQUE,
    agent_name TEXT NOT NULL,
    agent_id TEXT NOT NULL,
    status TEXT DEFAULT 'pending',  -- pending, approved, denied, expired
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    expires_at DATETIME NOT NULL,   -- request expires after 5 min
    session_token TEXT,             -- populated on approval
    session_expires_at DATETIME     -- session TTL (1 hour)
);

CREATE INDEX IF NOT EXISTS idx_agent_sessions_request ON agent_sessions(request_token);
CREATE INDEX IF NOT EXISTS idx_agent_sessions_session ON agent_sessions(session_token);
CREATE INDEX IF NOT EXISTS idx_agent_sessions_status ON agent_sessions(status);
CREATE INDEX IF NOT EXISTS idx_agents_agent_id ON agents(agent_id);
