"""Solo schema: local work queue, agent sessions, and teams membership tracking.

These tables exist in every Solo instance's solo-work.db. They are the
lightweight equivalent of the distributed coordinator's state, but
backed by SQLite instead of an HTTP server.

Note: The solo/db.py module creates these tables directly via its own
SOLO_WORK_SCHEMA constant. This module provides the canonical DDL for
use by the schema registry and migration system.
"""

SQLITE_DDL = """
-- Solo work items — local work queue for personal and team-sourced work.
CREATE TABLE IF NOT EXISTS solo_work_items (
    id TEXT PRIMARY KEY,
    title TEXT NOT NULL,
    description TEXT,
    status TEXT DEFAULT 'pending',
    priority INTEGER DEFAULT 5,
    project TEXT DEFAULT 'personal',
    feature TEXT DEFAULT 'general',
    task TEXT DEFAULT '',
    expert_type TEXT,
    assigned_agent TEXT,
    assigned_at TEXT,
    created_at TEXT DEFAULT (datetime('now')),
    completed_at TEXT,
    result TEXT,
    error TEXT,
    branch_name TEXT,
    pr_url TEXT,
    -- Teams integration: which team this work came from
    team_id TEXT,
    team_work_id TEXT,
    source TEXT DEFAULT 'local'
);

CREATE INDEX IF NOT EXISTS idx_solo_work_status ON solo_work_items(status);
CREATE INDEX IF NOT EXISTS idx_solo_work_team ON solo_work_items(team_id);
CREATE INDEX IF NOT EXISTS idx_solo_work_project ON solo_work_items(project);

-- Agent sessions — tracks locally spawned agents.
CREATE TABLE IF NOT EXISTS solo_agent_sessions (
    id TEXT PRIMARY KEY,
    work_item_id TEXT NOT NULL REFERENCES solo_work_items(id),
    agent_type TEXT DEFAULT 'implementer',
    soul_variant TEXT DEFAULT '',
    pid INTEGER,
    status TEXT DEFAULT 'running',
    started_at TEXT DEFAULT (datetime('now')),
    completed_at TEXT,
    exit_code INTEGER,
    log_path TEXT,
    cost_usd REAL DEFAULT 0.0
);

CREATE INDEX IF NOT EXISTS idx_solo_agent_status ON solo_agent_sessions(status);

-- Quality scores — records quality gate results per work item.
CREATE TABLE IF NOT EXISTS solo_quality_scores (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    work_item_id TEXT NOT NULL REFERENCES solo_work_items(id),
    agent_session_id TEXT REFERENCES solo_agent_sessions(id),
    tests_passed INTEGER DEFAULT 0,
    tests_failed INTEGER DEFAULT 0,
    lint_errors INTEGER DEFAULT 0,
    committed INTEGER DEFAULT 0,
    reviewed INTEGER DEFAULT 0,
    score REAL DEFAULT 0.0,
    created_at TEXT DEFAULT (datetime('now'))
);

-- Teams membership tracking — which teams this Solo is registered with.
CREATE TABLE IF NOT EXISTS solo_teams_memberships (
    team_id TEXT PRIMARY KEY,
    team_name TEXT NOT NULL,
    cluster_url TEXT NOT NULL,
    status TEXT DEFAULT 'registered',
    folder_scopes TEXT,
    joined_at TEXT DEFAULT (datetime('now')),
    last_checked_in TEXT,
    last_checked_out TEXT,
    last_sync_at TEXT,
    sync_version INTEGER DEFAULT 0
);

-- Solo configuration key-value store.
CREATE TABLE IF NOT EXISTS solo_config (
    key TEXT PRIMARY KEY,
    value TEXT NOT NULL,
    updated_at TEXT DEFAULT (datetime('now'))
);

-- Solo channels — logical containers for data classification and sync control.
-- The "personal" channel is created on first run and never syncs to teams.
CREATE TABLE IF NOT EXISTS solo_channels (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL UNIQUE,
    classification TEXT NOT NULL DEFAULT 'personal',
    encrypted INTEGER NOT NULL DEFAULT 1,
    sync_enabled INTEGER NOT NULL DEFAULT 0,
    description TEXT,
    created_at TEXT DEFAULT (datetime('now')),
    updated_at TEXT DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_solo_channels_classification
    ON solo_channels(classification);
"""

# Solo doesn't use CockroachDB — it's always SQLite.
# But we provide the function for schema registry compatibility.
CRDB_DDL = ""


def get_sqlite_ddl() -> str:
    return SQLITE_DDL


def get_crdb_ddl() -> str:
    return CRDB_DDL
