-- Migration: 001_initial_schema
-- Description: Creates the core Control Center database schema.

-- Enable UUID extension
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- ============================================
-- Table: agents
-- ============================================
CREATE TABLE agents (
    id            UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    name          TEXT NOT NULL,
    status        TEXT NOT NULL DEFAULT 'idle'
        CHECK (status IN ('active', 'idle', 'thinking', 'error', 'offline')),
    task          TEXT,
    progress      INT NOT NULL DEFAULT 0
        CHECK (progress >= 0 AND progress <= 100),
    session_key   TEXT,
    channel       TEXT,
    last_activity TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    created_at    TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at    TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

COMMENT ON TABLE agents IS 'Registered agents and their current state';
COMMENT ON COLUMN agents.status IS 'Agent lifecycle status: active, idle, thinking, error, offline';

-- ============================================
-- Table: sessions
-- ============================================
CREATE TABLE sessions (
    id         UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    agent_id   UUID NOT NULL,
    started_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    ended_at   TIMESTAMPTZ,
    status     TEXT NOT NULL DEFAULT 'running'
        CHECK (status IN ('running', 'completed', 'crashed', 'terminated')),
    CONSTRAINT fk_sessions_agent
        FOREIGN KEY (agent_id) REFERENCES agents(id)
        ON DELETE CASCADE
);

COMMENT ON TABLE sessions IS 'Agent session history';

-- ============================================
-- Table: task_logs
-- ============================================
CREATE TABLE task_logs (
    id             UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    agent_id       UUID NOT NULL,
    task           TEXT NOT NULL,
    status         TEXT NOT NULL DEFAULT 'pending'
        CHECK (status IN ('pending', 'running', 'completed', 'failed', 'cancelled')),
    started_at     TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    completed_at   TIMESTAMPTZ,
    error_message  TEXT,
    CONSTRAINT fk_task_logs_agent
        FOREIGN KEY (agent_id) REFERENCES agents(id)
        ON DELETE CASCADE
);

COMMENT ON TABLE task_logs IS 'Historical record of tasks assigned to agents';

-- ============================================
-- Table: projects
-- ============================================
CREATE TABLE projects (
    id          UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    name        TEXT NOT NULL,
    description TEXT,
    status      TEXT NOT NULL DEFAULT 'planned'
        CHECK (status IN ('planned', 'in_progress', 'completed', 'paused', 'cancelled')),
    agent_id    UUID,
    created_at  TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at  TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    CONSTRAINT fk_projects_agent
        FOREIGN KEY (agent_id) REFERENCES agents(id)
        ON DELETE SET NULL
);

COMMENT ON TABLE projects IS 'Projects managed by the Control Center';

-- ============================================
-- Table: agent_events
-- ============================================
CREATE TABLE agent_events (
    id         UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    agent_id   UUID NOT NULL,
    event_type TEXT NOT NULL,
    payload    JSONB,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    CONSTRAINT fk_agent_events_agent
        FOREIGN KEY (agent_id) REFERENCES agents(id)
        ON DELETE CASCADE
);

COMMENT ON TABLE agent_events IS 'Event stream for agent lifecycle and telemetry';
