from __future__ import annotations

from difflib import SequenceMatcher
from pathlib import Path
from typing import Any

from .render import render_page
from .util import load_json


def compare_pages(candidate_path: Path, golden_path: Path) -> dict[str, Any]:
    candidate = load_json(candidate_path)
    golden = load_json(golden_path)
    candidate_text = render_page(candidate, diplomatic=False)
    golden_text = render_page(golden, diplomatic=False)
    candidate_types = [block["type"] for block in candidate["blocks"]]
    golden_types = [block["type"] for block in golden["blocks"]]
    return {
        "page_id": golden["page_id"],
        "candidate": str(candidate_path),
        "diagnostic_fixture": str(golden_path),
        "fixture_authority": golden["review"]["authority"],
        "character_similarity": round(
            SequenceMatcher(None, candidate_text, golden_text, autojunk=False).ratio(),
            4,
        ),
        "candidate_character_count": len(candidate_text),
        "fixture_character_count": len(golden_text),
        "candidate_block_types": candidate_types,
        "fixture_block_types": golden_types,
        "block_types_match": candidate_types == golden_types,
        "candidate_editorial_note_count": len(candidate["editorial_notes"]),
        "fixture_editorial_note_count": len(golden["editorial_notes"]),
    }

