"""
Skill: Intelligence Analysis
==============================
Verify evidence aggregation, source reliability chains, and 
classification consistency.

Real-world use cases:
  - "Prove combining two intel reports preserves all source attributions"
  - "Verify evidence chain length is tracked correctly through merges"
  - "Show that report consolidation is order-independent"

Types:
  Report — EmptyReport | Entry(source, confidence, rest)

Functions:
  source_count(EmptyReport)         = Z
  source_count(Entry(s, c, rest))   = S(source_count(rest))

  consolidate(EmptyReport, r)           = r
  consolidate(Entry(s, c, rest), r)     = Entry(s, c, consolidate(rest, r))
"""

from nexus_gate_pkg.engine.skills import Skill, TheoremDef, register_skill
from nexus_gate_pkg.engine.terms import Var, Const, App, RewriteRule, Equation
from nexus_gate_pkg.engine.proof import register_induction_scheme
from nexus_gate_pkg.corpus.domain import Z, S, add


def EmptyReport():
    return Const("EmptyReport")

def Entry(source, confidence, rest):
    return App("Entry", [source, confidence, rest])

def source_count(r):
    return App("source_count", [r])

def consolidate(r1, r2):
    return App("consolidate", [r1, r2])


rpt, rpt2, rpt3 = Var("rpt"), Var("rpt2"), Var("rpt3")
s, c, rest = Var("s"), Var("c"), Var("rest")


INTEL_DEFS = [
    RewriteRule("source_count_empty", source_count(EmptyReport()), Z()),
    RewriteRule("source_count_entry", source_count(Entry(s, c, rest)),
                S(source_count(rest))),

    RewriteRule("consolidate_empty", consolidate(EmptyReport(), rpt), rpt),
    RewriteRule("consolidate_entry", consolidate(Entry(s, c, rest), rpt),
                Entry(s, c, consolidate(rest, rpt))),
]

def _thm(name, desc, lhs, rhs, ctx=None, diff="medium", tags=None):
    return TheoremDef(name, desc, lhs, rhs, context=ctx or [],
                      difficulty=diff, tags=tags or ["intel"])

intel_skill = Skill(
    id="intel",
    name="Intelligence Analysis",
    description="Verify evidence chains, source tracking, and report consolidation",
    version="1.0.0",
    definitions=INTEL_DEFS,
    theorems=[
        _thm("empty_report_clean",
             "An empty report has zero sources — verified clean slate",
             source_count(EmptyReport()), Z(),
             diff="trivial"),

        _thm("consolidation_preserves_sources",
             "Merging two reports preserves ALL source attributions — nothing lost in consolidation",
             source_count(consolidate(rpt, rpt2)),
             add(source_count(rpt), source_count(rpt2)),
             diff="medium"),

        _thm("consolidate_empty_noop",
             "Consolidating with an empty report changes nothing — safe operation",
             consolidate(rpt, EmptyReport()), rpt,
             diff="easy"),

        _thm("consolidation_order_independent",
             "Three-way consolidation gives same result regardless of grouping — no ordering bias",
             consolidate(consolidate(rpt, rpt2), rpt3),
             consolidate(rpt, consolidate(rpt2, rpt3)),
             diff="medium"),
    ],
    vocabulary={
        "EmptyReport": "empty intelligence report",
        "Entry": "evidence entry (source, confidence, rest)",
        "source_count": "number of sources",
        "consolidate": "merge reports",
    },
    examples=[
        "Are sources lost when consolidating reports?",
        "Is report merging order-independent?",
    ],
)

register_skill(intel_skill)

register_induction_scheme(
    type_name="Report",
    base_constructor="EmptyReport",
    rec_constructor="Entry",
    rec_arg_count=3,
    rec_positions=[2],
    var_hints={"rpt", "rpt2", "rpt3", "report", "intel"},
)
