"""Mesure l'effet de la passe d'alternance sur l'attribution (avant/apres).

Pour chaque modele : charge une fois, analyse le chapitre, intercepte les
locuteurs JUSTE avant `_repair_alternation` (etat "avant") puis lit l'etat
"apres", et score les deux contre la reference. Isole le gain de la passe
deterministe, independamment du cout du modele.
"""
from __future__ import annotations

import copy
import sys

from inkflow.analysis import segmenter
from inkflow.analysis.benchmark import _load_reference, _score_counts, _counts_to_score
from inkflow.analysis.llm.client import LLM
from inkflow.analysis.llm.factory import reset_llm_cache
from inkflow.epub.parser import load_book, load_chapter_text
from inkflow.store import artifacts

SLUG = "la-colere-de-tiamat"
CH = int(__import__("os").environ.get("DELTA_CH", "5"))


def main(model_ids: list[str]) -> None:
    book = load_book(SLUG)
    chapter = next(c for c in book.chapters if c.index == CH)
    ct = load_chapter_text(SLUG, chapter)
    cast = artifacts.load_cast(SLUG)
    ref = _load_reference(SLUG, CH)

    orig_repair = segmenter._repair_alternation
    print(f"{'modele':<40} {'avant':>7} {'apres':>7} {'delta':>7}")
    for model_id in model_ids:
        captured: dict[str, list] = {}

        def spy(segments, **kw):  # capture l'etat avant reparation
            captured["before"] = copy.deepcopy(segments)
            orig_repair(segments, **kw)

        segmenter._repair_alternation = spy
        try:
            gemma = LLM(model_id=model_id)
            analysis, _ = segmenter.analyze_chapter(
                chapter, ct, gemma, book_chars=list(cast.characters),
                dedup_gemma=None)
        finally:
            segmenter._repair_alternation = orig_repair
            reset_llm_cache()

        from inkflow.models import ChapterAnalysis
        before = ChapterAnalysis(index=CH, title=ct.title,
                                 segments=captured["before"])
        s_before = _counts_to_score(CH, _score_counts(ref, before, cast))
        s_after = _counts_to_score(CH, _score_counts(ref, analysis, cast))
        b = s_before.speaker_acc_dialogue
        a = s_after.speaker_acc_dialogue
        print(f"{model_id:<40} {b:>6.1%} {a:>6.1%} {a - b:>+6.1%}")


if __name__ == "__main__":
    main(sys.argv[1:] or ["mlx-community/gemma-3-4b-it-4bit"])
