"""Team Cognitive schema: shared observations, knowledge, patterns, and standards.

These tables live in the Teams DB (NOT in any Solo's cognitive.db).
Members explicitly contribute items from their personal cognitive memory
to the team's shared knowledge. Think: PR for knowledge.

Privacy model:
- Personal cognitive.db → auto-captured, never leaves device
- Team cognitive → explicit contribution only, visible to all checked-in members
- Contributors are tracked → attribution + accountability
- Items can be retracted by the contributor or team admin

Types of contributions:
- observation: A learning or insight from a dev session
- knowledge: Curated expertise (patterns, gotchas, reference material)
- pattern: A recurring behavior that agents should follow
- standard: A quality standard that all team work must meet
"""

SQLITE_DDL = """
-- Team-shared observations contributed by members.
CREATE TABLE IF NOT EXISTS team_observations (
    id TEXT PRIMARY KEY,
    title TEXT NOT NULL,
    content TEXT NOT NULL,
    category TEXT NOT NULL DEFAULT 'insight',
    domain TEXT DEFAULT 'general',
    confidence REAL DEFAULT 0.7,
    -- Contribution metadata
    contributed_by TEXT NOT NULL,
    contributor_name TEXT DEFAULT '',
    source_observation_id TEXT,
    contributed_at TEXT DEFAULT (datetime('now')),
    -- Lifecycle
    status TEXT DEFAULT 'active',
    confirmations INTEGER DEFAULT 0,
    applications INTEGER DEFAULT 0,
    retracted_at TEXT,
    retracted_by TEXT
);

CREATE INDEX IF NOT EXISTS idx_team_obs_domain ON team_observations(domain);
CREATE INDEX IF NOT EXISTS idx_team_obs_contributor ON team_observations(contributed_by);
CREATE INDEX IF NOT EXISTS idx_team_obs_status ON team_observations(status);

-- Team-shared knowledge entries.
CREATE TABLE IF NOT EXISTS team_knowledge (
    id TEXT PRIMARY KEY,
    entry_type TEXT NOT NULL DEFAULT 'reference',
    title TEXT NOT NULL,
    content TEXT NOT NULL,
    domain TEXT DEFAULT 'general',
    scope TEXT DEFAULT 'team-wide',
    confidence REAL DEFAULT 0.7,
    tags TEXT DEFAULT '',
    -- Contribution metadata
    contributed_by TEXT NOT NULL,
    contributor_name TEXT DEFAULT '',
    source_knowledge_id TEXT,
    contributed_at TEXT DEFAULT (datetime('now')),
    hlc_timestamp TEXT DEFAULT '',
    -- Sync tracking
    sync_version INTEGER DEFAULT 0,
    classification_level INTEGER DEFAULT 0,
    -- Lifecycle
    status TEXT DEFAULT 'active',
    applications INTEGER DEFAULT 0,
    updated_at TEXT DEFAULT (datetime('now')),
    retracted_at TEXT,
    retracted_by TEXT
);

CREATE INDEX IF NOT EXISTS idx_team_knowledge_domain ON team_knowledge(domain);
CREATE INDEX IF NOT EXISTS idx_team_knowledge_type ON team_knowledge(entry_type);
CREATE INDEX IF NOT EXISTS idx_team_knowledge_contributor ON team_knowledge(contributed_by);
CREATE INDEX IF NOT EXISTS idx_team_knowledge_sync_version ON team_knowledge(sync_version);
CREATE INDEX IF NOT EXISTS idx_team_knowledge_source ON team_knowledge(source_knowledge_id);

-- Team-shared patterns.
CREATE TABLE IF NOT EXISTS team_patterns (
    id TEXT PRIMARY KEY,
    type TEXT NOT NULL,
    domain TEXT NOT NULL DEFAULT 'general',
    title TEXT NOT NULL,
    observation TEXT NOT NULL,
    confidence TEXT DEFAULT 'medium',
    -- Contribution metadata
    contributed_by TEXT NOT NULL,
    contributor_name TEXT DEFAULT '',
    source_pattern_id TEXT,
    contributed_at TEXT DEFAULT (datetime('now')),
    -- Lifecycle
    status TEXT DEFAULT 'active',
    confirmations INTEGER DEFAULT 0,
    applications INTEGER DEFAULT 0,
    retracted_at TEXT,
    retracted_by TEXT
);

CREATE INDEX IF NOT EXISTS idx_team_patterns_domain ON team_patterns(domain);
CREATE INDEX IF NOT EXISTS idx_team_patterns_contributor ON team_patterns(contributed_by);

-- Team quality standards.
CREATE TABLE IF NOT EXISTS team_standards (
    id TEXT PRIMARY KEY,
    title TEXT NOT NULL,
    description TEXT NOT NULL,
    category TEXT DEFAULT 'code-quality',
    severity TEXT DEFAULT 'required',
    -- Contribution metadata
    contributed_by TEXT NOT NULL,
    contributor_name TEXT DEFAULT '',
    contributed_at TEXT DEFAULT (datetime('now')),
    -- Lifecycle
    status TEXT DEFAULT 'active',
    retracted_at TEXT,
    retracted_by TEXT
);

CREATE INDEX IF NOT EXISTS idx_team_standards_category ON team_standards(category);

-- Contribution log — audit trail of all contributions and retractions.
CREATE TABLE IF NOT EXISTS contribution_log (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    action TEXT NOT NULL,
    item_type TEXT NOT NULL,
    item_id TEXT NOT NULL,
    member_id TEXT NOT NULL,
    member_name TEXT DEFAULT '',
    details TEXT,
    created_at TEXT DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_contribution_log_member ON contribution_log(member_id);
CREATE INDEX IF NOT EXISTS idx_contribution_log_item ON contribution_log(item_id);

-- Node health — heartbeat tracking for Solo nodes in the cognitive sync mesh.
-- Status is computed at read time based on last_heartbeat_at:
--   online: < 15 min since heartbeat
--   stale:  15-30 min since heartbeat
--   offline: > 30 min since heartbeat
CREATE TABLE IF NOT EXISTS node_health (
    node_id TEXT PRIMARY KEY,
    hostname TEXT NOT NULL DEFAULT '',
    db_size_mb REAL DEFAULT 0,
    observation_count INTEGER DEFAULT 0,
    last_sync_version INTEGER DEFAULT 0,
    last_heartbeat_at TEXT NOT NULL DEFAULT (datetime('now')),
    first_seen_at TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_node_health_heartbeat ON node_health(last_heartbeat_at);
"""

# CockroachDB variant — replace SQLite-specific syntax
CRDB_DDL = SQLITE_DDL.replace(
    "INTEGER PRIMARY KEY AUTOINCREMENT", "INT8 PRIMARY KEY DEFAULT unique_rowid()"
).replace(
    "datetime('now')", "now()"
).replace(
    "TEXT DEFAULT (now())", "TIMESTAMPTZ DEFAULT now()"
)


def get_sqlite_ddl() -> str:
    return SQLITE_DDL


def get_crdb_ddl() -> str:
    return CRDB_DDL
