#!/usr/bin/env python3
"""
Run the LangGraph incident-kernel offline replay adapter.

This command is deterministic and local. It does not install LangGraph, call an
LLM, execute tools, mutate production systems, or read fixture labels.
"""

from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path
from typing import Any


ROOT = Path(__file__).resolve().parents[2]
API_SRC = ROOT / "apps" / "api"
sys.path.insert(0, str(API_SRC))

from src.services.agent_langgraph_adapter import (  # noqa: E402
    LANGGRAPH_CANDIDATE_ID,
    build_langgraph_candidate_results,
)


def main() -> int:
    parser = argparse.ArgumentParser(
        description="Run LangGraph incident-kernel offline replay."
    )
    parser.add_argument("--inputs", required=True, help="candidate input JSONL")
    parser.add_argument("--output", required=True, help="candidate raw result JSONL")
    parser.add_argument("--report", help="optional aggregate adapter report JSON")
    args = parser.parse_args()

    candidate_inputs = _read_jsonl(Path(args.inputs))
    decisions = build_langgraph_candidate_results(candidate_inputs)
    with Path(args.output).open("w", encoding="utf-8") as handle:
        for decision in decisions:
            handle.write(json.dumps(decision.to_dict(), ensure_ascii=False, sort_keys=True))
            handle.write("\n")

    report = {
        "schema_version": "agent_langgraph_replay_adapter_report_v1",
        "candidate_id": LANGGRAPH_CANDIDATE_ID,
        "inputs": args.inputs,
        "output": args.output,
        "records": len(decisions),
        "external_calls": False,
        "tools_executed": False,
        "production_writes": False,
        "fixture_labels_read": False,
        "sdk_dependency": "langgraph_python_package_not_installed",
        "adapter_mode": "deterministic_offline_workflow_kernel",
    }
    if args.report:
        Path(args.report).write_text(
            json.dumps(report, ensure_ascii=False, indent=2, sort_keys=True) + "\n",
            encoding="utf-8",
        )
    print(json.dumps(report, ensure_ascii=False, sort_keys=True))
    return 0


def _read_jsonl(path: Path) -> list[dict[str, Any]]:
    records: list[dict[str, Any]] = []
    with path.open(encoding="utf-8") as handle:
        for line_number, line in enumerate(handle, start=1):
            line = line.strip()
            if not line or line.startswith("#"):
                continue
            try:
                payload = json.loads(line)
            except Exception as exc:
                raise SystemExit(f"{path}:{line_number}: invalid JSONL: {exc}") from exc
            if not isinstance(payload, dict):
                raise SystemExit(f"{path}:{line_number}: expected JSON object")
            records.append(payload)
    return records


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