from __future__ import annotations

import sqlite3
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parents[1]
SCHEMA_SQL = REPO_ROOT / "schemas" / "teleo-agent-graph-v1.sql"


def test_agent_graph_schema_applies_and_models_shared_nodes():
    conn = sqlite3.connect(":memory:")
    conn.row_factory = sqlite3.Row
    conn.execute("PRAGMA foreign_keys = ON")
    conn.executescript(SCHEMA_SQL.read_text())

    conn.executemany(
        "INSERT INTO agents (slug, display_name, archetype) VALUES (?, ?, ?)",
        [
            ("leo", "Leo", "cross-domain synthesizer"),
            ("theseus", "Theseus", "AI alignment"),
        ],
    )
    conn.execute(
        """INSERT INTO agent_persona_revisions
           (id, agent_slug, revision, identity, voice, role, authored_by)
           VALUES
           ('persona-leo-v1', 'leo', 1, 'cross-domain synthesizer', 'direct', 'evaluate commons', 'diagram'),
           ('persona-theseus-v1', 'theseus', 1, 'alignment maze navigator', 'precise', 'AI evidence lead', 'diagram')"""
    )
    conn.execute(
        """INSERT INTO agent_strategy_revisions
           (id, agent_slug, persona_revision_id, revision, diagnosis, guiding_policy, proximate_objectives_json)
           VALUES
           ('strategy-leo-v1', 'leo', 'persona-leo-v1', 1, 'coordination is the bottleneck', 'surface cross-domain isomorphisms', '[]'),
           ('strategy-theseus-v1', 'theseus', 'persona-theseus-v1', 1, 'AI discourse is ungrounded', 'separate generation from evaluation', '[]')"""
    )
    conn.executemany(
        """INSERT INTO evidence
           (id, evidence_type, title, summary, verification_status)
           VALUES (?, ?, ?, ?, 'verified')""",
        [
            ("e-kim-2025", "study", "Kim et al. ICML 2025", "Shared evidence grounding coordination and verification degradation."),
            ("e-arrow", "formal_result", "Arrow impossibility theorem", "Formal result grounding alignment impossibility claim."),
        ],
    )
    conn.executemany(
        """INSERT INTO claims
           (id, slug, domain, description, confidence, primary_evidence_id, status)
           VALUES (?, ?, ?, ?, ?, ?, 'accepted')""",
        [
            ("c-coordination", "alignment-is-coordination", "ai-alignment", "Alignment is a coordination problem, not only a technical one.", "likely", "e-kim-2025"),
            ("c-verification", "verification-degrades-with-capability", "ai-alignment", "Verification degrades as capability gaps grow.", "experimental", "e-kim-2025"),
            ("c-arrow", "universal-alignment-impossible", "ai-alignment", "Universal alignment is mathematically impossible under strong aggregation assumptions.", "likely", "e-arrow"),
        ],
    )
    conn.executemany(
        """INSERT INTO claim_evidence_edges
           (id, claim_id, evidence_id, relation, weight, rationale)
           VALUES (?, ?, ?, 'supports', ?, ?)""",
        [
            ("ce-kim-coordination", "c-coordination", "e-kim-2025", 0.9, "Diagram shared-node case: one evidence node grounds multiple claims."),
            ("ce-kim-verification", "c-verification", "e-kim-2025", 0.8, "Same evidence also grounds verification degradation."),
            ("ce-arrow", "c-arrow", "e-arrow", 0.9, "Formal result evidence."),
        ],
    )
    conn.executemany(
        """INSERT INTO agent_beliefs
           (id, agent_slug, belief_code, title, statement, falsification_criteria, is_keystone)
           VALUES (?, ?, ?, ?, ?, ?, ?)""",
        [
            ("b-leo-b1", "leo", "B1", "Coordination bottleneck", "Coordination is the bottleneck.", "Falsified by civ-scale pure-tech solution.", 1),
            ("b-theseus-t2", "theseus", "T2", "Alignment as coordination", "Alignment is a coordination problem.", "Falsified by a robust one-agent technical alignment solution.", 1),
            ("b-theseus-t4", "theseus", "T4", "Verification degradation", "Verification degrades faster than capability grows.", "Falsified by scalable oversight evidence.", 0),
        ],
    )
    conn.executemany(
        """INSERT INTO belief_claim_edges
           (id, belief_id, claim_id, relation, weight, rationale)
           VALUES (?, ?, ?, 'cites', ?, ?)""",
        [
            ("bc-leo-coordination", "b-leo-b1", "c-coordination", 1.0, "Keystone belief cites shared claim."),
            ("bc-theseus-coordination", "b-theseus-t2", "c-coordination", 0.9, "Different agent cites same shared claim."),
            ("bc-theseus-verification", "b-theseus-t4", "c-verification", 0.9, "Belief cites verification claim."),
            ("bc-theseus-arrow", "b-theseus-t2", "c-arrow", 0.6, "Belief also cites formal-result claim."),
        ],
    )
    conn.execute(
        """INSERT INTO claim_edges
           (id, from_claim_id, to_claim_id, relation, weight, rationale)
           VALUES ('edge-verification-supports-coordination', 'c-verification', 'c-coordination', 'supports', 0.6, 'Oversight degradation strengthens coordination framing.')"""
    )
    conn.execute(
        """INSERT INTO cascade_events
           (id, changed_layer, changed_id, affected_layer, affected_id, reason)
           VALUES ('cascade-kim-to-coordination', 'evidence', 'e-kim-2025', 'claim', 'c-coordination', 'shared evidence updated')"""
    )

    shared_evidence_count = conn.execute(
        "SELECT COUNT(*) AS n FROM claim_evidence_edges WHERE evidence_id = 'e-kim-2025'"
    ).fetchone()["n"]
    shared_claim_count = conn.execute(
        "SELECT COUNT(*) AS n FROM belief_claim_edges WHERE claim_id = 'c-coordination'"
    ).fetchone()["n"]
    cascade_count = conn.execute("SELECT COUNT(*) AS n FROM cascade_events").fetchone()["n"]

    assert shared_evidence_count == 2
    assert shared_claim_count == 2
    assert cascade_count == 1


def test_claim_edges_reject_self_reference():
    conn = sqlite3.connect(":memory:")
    conn.execute("PRAGMA foreign_keys = ON")
    conn.executescript(SCHEMA_SQL.read_text())
    conn.execute(
        """INSERT INTO claims (id, slug, domain, description)
           VALUES ('c1', 'claim-one', 'ai-alignment', 'A claim specific enough to disagree with.')"""
    )

    try:
        conn.execute(
            """INSERT INTO claim_edges
               (id, from_claim_id, to_claim_id, relation, rationale)
               VALUES ('self', 'c1', 'c1', 'related', 'self edge should fail')"""
        )
    except sqlite3.IntegrityError:
        pass
    else:
        raise AssertionError("claim_edges allowed a self-reference")
