"""
Run the StateBench enterprise eval contract smoke suite.

This is the repo-local command a GitLab runner can execute before publishing
eval artifacts to a team or firm-wide collector.
"""
from __future__ import annotations

import json
import tempfile
from dataclasses import dataclass
from pathlib import Path

from statebench.runners.contract_validator import validate_file
from statebench.runners.eval_rollup_collector import main as generate_eval_rollups
from statebench.runners.validate_eval_gap_backlog import main as validate_eval_gap_backlog
from statebench.runners.validate_eval_source_ledger import main as validate_eval_source_ledger
from statebench.runners.validate_gitlab_eval_component import main as validate_gitlab_eval_component

REPO_ROOT = Path(__file__).resolve().parents[2]
FIXTURE_ROOT = REPO_ROOT / "statebench" / "fixtures" / "enterprise-eval-contract" / "v1"


@dataclass(frozen=True)
class Check:
    name: str
    schema: str
    path: Path
    jsonl: bool
    expect_valid: bool


CHECKS = [
    Check("eval cases pass", "eval_case_v1", FIXTURE_ROOT / "eval-cases.pass.jsonl", True, True),
    Check("eval results pass", "eval_result_v1", FIXTURE_ROOT / "eval-results.pass.jsonl", True, True),
    Check("gitlab annotations pass", "gitlab_eval_annotation_v1", FIXTURE_ROOT / "gitlab-annotations.pass.json", False, True),
    Check("native fixtures pass", "native_eval_fixture_v1", FIXTURE_ROOT / "native-fixtures.pass.jsonl", True, True),
    Check(
        "eval source gaps pass",
        "eval_source_gap_v1",
        REPO_ROOT / "sources" / "21-benchmarks" / "eval-native-schema-gap-backlog-2026.jsonl",
        True,
        True,
    ),
    Check("team rollup pass", "team_eval_rollup_v1", FIXTURE_ROOT / "team-rollup.pass.json", False, True),
    Check("firm rollup pass", "firm_eval_rollup_v1", FIXTURE_ROOT / "firm-rollup.pass.json", False, True),
    Check("eval results fail", "eval_result_v1", FIXTURE_ROOT / "eval-results.fail.jsonl", True, False),
    Check("native fixtures fail", "native_eval_fixture_v1", FIXTURE_ROOT / "native-fixtures.fail.jsonl", True, False),
]


def main() -> int:
    failed: list[str] = []
    for check in CHECKS:
        errors = validate_file(check.schema, check.path, jsonl=check.jsonl)
        valid = not errors
        if valid != check.expect_valid:
            expectation = "valid" if check.expect_valid else "invalid"
            detail = "; ".join(errors[:3]) if errors else "no validation errors"
            failed.append(f"{check.name}: expected {expectation}, got {'valid' if valid else 'invalid'} ({detail})")
            continue
        status = "valid" if valid else "invalid as expected"
        print(f"ok - {check.name}: {status}")

    if validate_gitlab_eval_component() != 0:
        failed.append("gitlab eval component: required markers missing")

    if validate_eval_source_ledger() != 0:
        failed.append("eval source ledger: external real-run export inventory is inconsistent")

    if validate_eval_gap_backlog() != 0:
        failed.append("eval source gap backlog: source-ledger remaining gaps are inconsistent")

    with tempfile.TemporaryDirectory() as temp_dir:
        temp_root = Path(temp_dir)
        generated_team = temp_root / "team-rollup.generated.json"
        generated_firm = temp_root / "firm-rollup.generated.json"
        collector_status = generate_eval_rollups(
            [
                "--eval-results",
                str(FIXTURE_ROOT / "eval-results.pass.jsonl"),
                "--team-id",
                "hr-platform",
                "--team-rollup-id",
                "team_hr_platform_20260622",
                "--firm-rollup-id",
                "firm_eval_rollup_20260622",
                "--window-start-utc",
                "2026-06-22T00:00:00Z",
                "--window-end-utc",
                "2026-06-22T23:59:59Z",
                "--created-at-utc",
                "2026-06-22T11:25:00Z",
                "--team-output",
                str(generated_team),
                "--firm-output",
                str(generated_firm),
                "--team-notes-ref",
                "reports/team-summary.md",
            ]
        )
        if collector_status != 0:
            failed.append("eval rollup collector: generation failed")
        else:
            expected_team = json.loads((FIXTURE_ROOT / "team-rollup.pass.json").read_text(encoding="utf-8"))
            expected_firm = json.loads((FIXTURE_ROOT / "firm-rollup.pass.json").read_text(encoding="utf-8"))
            actual_team = json.loads(generated_team.read_text(encoding="utf-8"))
            actual_firm = json.loads(generated_firm.read_text(encoding="utf-8"))
            if actual_team != expected_team:
                failed.append("eval rollup collector: generated team rollup differs from fixture")
            else:
                print("ok - eval rollup collector: generated team rollup matches fixture")
            if actual_firm != expected_firm:
                failed.append("eval rollup collector: generated firm rollup differs from fixture")
            else:
                print("ok - eval rollup collector: generated firm rollup matches fixture")

    if failed:
        for item in failed:
            print(f"error - {item}")
        return 1
    print("ok - enterprise eval contract smoke suite passed")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
