-- Phase-2: Experience Learner lessons store (append-only)

CREATE TABLE IF NOT EXISTS agent_lessons (
    id BIGSERIAL PRIMARY KEY,
    lesson_id UUID NOT NULL UNIQUE,
    lesson_key TEXT NOT NULL UNIQUE,
    ts TIMESTAMPTZ NOT NULL,
    scope TEXT NOT NULL,
    agent_id TEXT NULL,
    task_type TEXT NOT NULL,
    trigger TEXT NOT NULL,
    action TEXT NOT NULL,
    avoid TEXT NOT NULL,
    signals JSONB NOT NULL,
    evidence JSONB NOT NULL,
    raw JSONB NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_agent_lessons_scope_ts
    ON agent_lessons (scope, ts DESC);

CREATE INDEX IF NOT EXISTS idx_agent_lessons_agent_ts
    ON agent_lessons (agent_id, ts DESC);

CREATE INDEX IF NOT EXISTS idx_agent_lessons_task_ts
    ON agent_lessons (task_type, ts DESC);
