"""Teams schema: team members, folder scoping, auth, and team coordination.

These tables extend the distributed schema with Teams-specific concepts.
They exist only in Teams deployments (libsql/sqld or CockroachDB).

The distributed schema tables (agents, worktrees, work_queue, etc.) are
still used — this module adds the membership and access control layer.
"""

SQLITE_DDL = """
-- Teams membership — tracks Solo instances connected to a Teams cluster.
CREATE TABLE IF NOT EXISTS team_members (
    id TEXT PRIMARY KEY,
    solo_id TEXT NOT NULL UNIQUE,
    display_name TEXT NOT NULL,
    email TEXT DEFAULT '',
    role TEXT DEFAULT 'developer',
    status TEXT DEFAULT 'registered',
    checked_in_at TEXT,
    last_heartbeat TEXT,
    mfa_verified INTEGER DEFAULT 0,
    mfa_method TEXT DEFAULT '',
    tailscale_ip TEXT DEFAULT '',
    solo_version TEXT DEFAULT '',
    joined_at TEXT DEFAULT (datetime('now')),
    updated_at TEXT DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_team_members_solo ON team_members(solo_id);
CREATE INDEX IF NOT EXISTS idx_team_members_status ON team_members(status);

-- Folder scoping — defines what each member's agents can access.
CREATE TABLE IF NOT EXISTS folder_scopes (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    member_id TEXT NOT NULL REFERENCES team_members(id),
    path TEXT NOT NULL,
    access_level TEXT DEFAULT 'readwrite',
    created_at TEXT DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_folder_scopes_member ON folder_scopes(member_id);

-- GitHub integration — per-member repo access granted on check-in.
CREATE TABLE IF NOT EXISTS github_access (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    member_id TEXT NOT NULL REFERENCES team_members(id),
    github_username TEXT NOT NULL,
    repo TEXT NOT NULL,
    permission TEXT DEFAULT 'write',
    granted_at TEXT DEFAULT (datetime('now')),
    revoked_at TEXT
);

CREATE INDEX IF NOT EXISTS idx_github_access_member ON github_access(member_id);

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

-- Auth sessions — server-side session token tracking.
CREATE TABLE IF NOT EXISTS auth_sessions (
    token_hash TEXT PRIMARY KEY,
    member_id TEXT NOT NULL REFERENCES team_members(id),
    solo_id TEXT NOT NULL,
    issued_at TEXT DEFAULT (datetime('now')),
    expires_at TEXT,
    revoked INTEGER DEFAULT 0,
    last_used_at TEXT
);

CREATE INDEX IF NOT EXISTS idx_auth_sessions_member ON auth_sessions(member_id);

-- Dedicated agent nodes — servers in the Teams cluster that run agents.
CREATE TABLE IF NOT EXISTS agent_nodes (
    id TEXT PRIMARY KEY,
    hostname TEXT NOT NULL,
    tailscale_ip TEXT DEFAULT '',
    status TEXT DEFAULT 'online',
    max_concurrent_agents INTEGER DEFAULT 7,
    current_agent_count INTEGER DEFAULT 0,
    gpu_available INTEGER DEFAULT 0,
    disk_gb INTEGER DEFAULT 0,
    ram_gb INTEGER DEFAULT 0,
    last_heartbeat TEXT,
    created_at TEXT DEFAULT (datetime('now'))
);

-- Work assignment — tracks which member or node is assigned team work.
CREATE TABLE IF NOT EXISTS work_assignments (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    work_item_id TEXT NOT NULL,
    assigned_to TEXT NOT NULL,
    assignment_type TEXT DEFAULT 'local',
    folder_scope TEXT,
    assigned_at TEXT DEFAULT (datetime('now')),
    completed_at TEXT
);

CREATE INDEX IF NOT EXISTS idx_work_assignments_item ON work_assignments(work_item_id);
CREATE INDEX IF NOT EXISTS idx_work_assignments_member ON work_assignments(assigned_to);

-- Sync log — tracks data synchronization between Solo instances and Teams.
CREATE TABLE IF NOT EXISTS sync_log (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    member_id TEXT NOT NULL,
    direction TEXT NOT NULL,
    table_name TEXT NOT NULL,
    row_count INTEGER DEFAULT 0,
    synced_at TEXT DEFAULT (datetime('now'))
);
"""

CRDB_DDL = """
CREATE TABLE IF NOT EXISTS team_members (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    solo_id TEXT NOT NULL UNIQUE,
    display_name TEXT NOT NULL,
    email TEXT DEFAULT '',
    role TEXT DEFAULT 'developer',
    status TEXT DEFAULT 'registered',
    checked_in_at TIMESTAMPTZ,
    last_heartbeat TIMESTAMPTZ,
    mfa_verified BOOL DEFAULT FALSE,
    mfa_method TEXT DEFAULT '',
    tailscale_ip TEXT DEFAULT '',
    solo_version TEXT DEFAULT '',
    joined_at TIMESTAMPTZ DEFAULT now(),
    updated_at TIMESTAMPTZ DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_team_members_solo ON team_members(solo_id);
CREATE INDEX IF NOT EXISTS idx_team_members_status ON team_members(status);

CREATE TABLE IF NOT EXISTS folder_scopes (
    id INT8 PRIMARY KEY DEFAULT unique_rowid(),
    member_id UUID NOT NULL REFERENCES team_members(id),
    path TEXT NOT NULL,
    access_level TEXT DEFAULT 'readwrite',
    created_at TIMESTAMPTZ DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_folder_scopes_member ON folder_scopes(member_id);

CREATE TABLE IF NOT EXISTS github_access (
    id INT8 PRIMARY KEY DEFAULT unique_rowid(),
    member_id UUID NOT NULL REFERENCES team_members(id),
    github_username TEXT NOT NULL,
    repo TEXT NOT NULL,
    permission TEXT DEFAULT 'write',
    granted_at TIMESTAMPTZ DEFAULT now(),
    revoked_at TIMESTAMPTZ
);

CREATE INDEX IF NOT EXISTS idx_github_access_member ON github_access(member_id);

CREATE TABLE IF NOT EXISTS team_config (
    key TEXT PRIMARY KEY,
    value TEXT NOT NULL,
    updated_at TIMESTAMPTZ DEFAULT now()
);

CREATE TABLE IF NOT EXISTS auth_sessions (
    token_hash TEXT PRIMARY KEY,
    member_id UUID NOT NULL REFERENCES team_members(id),
    solo_id TEXT NOT NULL,
    issued_at TIMESTAMPTZ DEFAULT now(),
    expires_at TIMESTAMPTZ,
    revoked BOOL DEFAULT FALSE,
    last_used_at TIMESTAMPTZ
);

CREATE INDEX IF NOT EXISTS idx_auth_sessions_member ON auth_sessions(member_id);

CREATE TABLE IF NOT EXISTS agent_nodes (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    hostname TEXT NOT NULL,
    tailscale_ip TEXT DEFAULT '',
    status TEXT DEFAULT 'online',
    max_concurrent_agents INT DEFAULT 7,
    current_agent_count INT DEFAULT 0,
    gpu_available BOOL DEFAULT FALSE,
    disk_gb INT DEFAULT 0,
    ram_gb INT DEFAULT 0,
    last_heartbeat TIMESTAMPTZ,
    created_at TIMESTAMPTZ DEFAULT now()
);

CREATE TABLE IF NOT EXISTS work_assignments (
    id INT8 PRIMARY KEY DEFAULT unique_rowid(),
    work_item_id UUID NOT NULL,
    assigned_to TEXT NOT NULL,
    assignment_type TEXT DEFAULT 'local',
    folder_scope TEXT,
    assigned_at TIMESTAMPTZ DEFAULT now(),
    completed_at TIMESTAMPTZ
);

CREATE INDEX IF NOT EXISTS idx_work_assignments_item ON work_assignments(work_item_id);
CREATE INDEX IF NOT EXISTS idx_work_assignments_member ON work_assignments(assigned_to);

CREATE TABLE IF NOT EXISTS sync_log (
    id INT8 PRIMARY KEY DEFAULT unique_rowid(),
    member_id UUID NOT NULL,
    direction TEXT NOT NULL,
    table_name TEXT NOT NULL,
    row_count INT DEFAULT 0,
    synced_at TIMESTAMPTZ DEFAULT now()
);
"""


def get_sqlite_ddl() -> str:
    return SQLITE_DDL


def get_crdb_ddl() -> str:
    return CRDB_DDL
