from __future__ import annotations

import json
import sys
from pathlib import Path

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

import report_repeated_failures as repeated_script  # noqa: E402


def write_jsonl(path: Path, records: list[dict]) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    with path.open("w", encoding="utf-8") as handle:
        for record in records:
            handle.write(json.dumps(record, ensure_ascii=False) + "\n")


def test_report_repeated_failures_groups_by_failure_type_and_prompt(tmp_path: Path) -> None:
    root = tmp_path / "reports" / "observability" / "onec-agent" / "turn_audit" / "20260705.jsonl"
    write_jsonl(
        root,
        [
            {
                "timestamp": "2026-07-01T10:00:00+00:00",
                "turn_id": "a1",
                "request_id": "r1",
                "outcome": "failure",
                "failure_type": "adapter_error",
                "error_code": "adapter_error",
                "error_message": "missing base_id",
                "user_text": "Проверь   сохраненное   состояние",
            },
            {
                "timestamp": "2026-07-02T10:00:00+00:00",
                "turn_id": "a2",
                "request_id": "r2",
                "outcome": "failure",
                "failure_type": "adapter_error",
                "error_code": "adapter_error",
                "error_message": "missing base_id",
                "user_text": "Проверь сохраненное состояние",
            },
            {
                "timestamp": "2026-07-03T10:00:00+00:00",
                "turn_id": "ok1",
                "request_id": "r3",
                "outcome": "success",
                "failure_type": "none",
                "user_text": "ok",
            },
        ],
    )

    records = repeated_script.iter_turn_audit(tmp_path / "reports" / "observability")
    groups = repeated_script.group_repeated_failures(records, min_count=2, limit=10)

    assert len(groups) == 1
    assert groups[0]["count"] == 2
    assert groups[0]["failure_type"] == "adapter_error"
    assert groups[0]["normalized_user_text"] == "Проверь сохраненное состояние"
    assert groups[0]["first_seen"] == "2026-07-01T10:00:00+00:00"
    assert groups[0]["last_seen"] == "2026-07-02T10:00:00+00:00"


def test_report_repeated_failures_writes_output_file(tmp_path: Path) -> None:
    output = tmp_path / "repeated-failures.json"
    payload = {"records": 2, "repeated_failures": []}
    repeated_script.write_report(output, payload)
    assert json.loads(output.read_text(encoding="utf-8"))["records"] == 2
