"""
AI Processing - Print Version Generator: Cloze (Lueckentext).

Generates printable HTML for cloze/fill-in-the-blank worksheets.
"""

from pathlib import Path
import json
import random
import logging

from .core import BEREINIGT_DIR

logger = logging.getLogger(__name__)


def generate_print_version_cloze(cloze_path: Path, include_answers: bool = False) -> Path:
    """
    Generiert eine druckbare HTML-Version der Lueckentexte.

    Args:
        cloze_path: Pfad zur *_cloze.json Datei
        include_answers: True fuer Loesungsblatt (fuer Eltern)

    Returns:
        Pfad zur generierten HTML-Datei
    """
    if not cloze_path.exists():
        raise FileNotFoundError(f"Cloze-Datei nicht gefunden: {cloze_path}")

    cloze_data = json.loads(cloze_path.read_text(encoding="utf-8"))
    items = cloze_data.get("cloze_items", [])
    metadata = cloze_data.get("metadata", {})

    title = metadata.get("source_title", "Arbeitsblatt")
    subject = metadata.get("subject", "")
    grade = metadata.get("grade_level", "")
    total_gaps = metadata.get("total_gaps", 0)

    html_parts = []
    html_parts.append("""<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>""" + title + """ - Lueckentext</title>
<style>
  @media print {
    .no-print { display: none; }
    .page-break { page-break-before: always; }
  }
  body {
    font-family: Arial, sans-serif;
    max-width: 800px;
    margin: 40px auto;
    padding: 20px;
    line-height: 1.8;
  }
  h1 { font-size: 24px; margin-bottom: 8px; }
  .meta { color: #666; margin-bottom: 24px; }
  .cloze-item {
    margin-bottom: 24px;
    padding: 16px;
    background: #f9f9f9;
    border-radius: 8px;
  }
  .cloze-number {
    font-weight: bold;
    color: #333;
    margin-bottom: 8px;
  }
  .cloze-sentence {
    font-size: 16px;
    line-height: 2;
  }
  .gap {
    display: inline-block;
    min-width: 80px;
    border-bottom: 2px solid #333;
    margin: 0 4px;
    text-align: center;
  }
  .gap-filled {
    display: inline-block;
    padding: 2px 8px;
    background: #e8f5e9;
    border: 1px solid #4caf50;
    border-radius: 4px;
    font-weight: bold;
  }
  .translation {
    margin-top: 12px;
    padding: 8px;
    background: #e3f2fd;
    border-left: 3px solid #2196f3;
    font-size: 14px;
    color: #555;
  }
  .translation-label {
    font-size: 12px;
    color: #777;
    margin-bottom: 4px;
  }
  .word-bank {
    margin-top: 32px;
    padding: 16px;
    background: #fff3e0;
    border-radius: 8px;
  }
  .word-bank-title {
    font-weight: bold;
    margin-bottom: 12px;
  }
  .word {
    display: inline-block;
    padding: 4px 12px;
    margin: 4px;
    background: white;
    border: 1px solid #ddd;
    border-radius: 4px;
  }
</style>
</head>
<body>
""")

    # Header
    version_text = "Loesungsblatt" if include_answers else "Lueckentext"
    html_parts.append(f"<h1>{title} - {version_text}</h1>")
    meta_parts = []
    if subject:
        meta_parts.append(f"Fach: {subject}")
    if grade:
        meta_parts.append(f"Klasse: {grade}")
    meta_parts.append(f"Luecken gesamt: {total_gaps}")
    html_parts.append(f"<div class='meta'>{' | '.join(meta_parts)}</div>")

    # Sammle alle Lueckenwoerter fuer Wortbank
    all_words = []

    # Lueckentexte
    for idx, item in enumerate(items, 1):
        html_parts.append("<div class='cloze-item'>")
        html_parts.append(f"<div class='cloze-number'>{idx}.</div>")

        gaps = item.get("gaps", [])
        sentence = item.get("sentence_with_gaps", "")

        if include_answers:
            # Loesungsblatt: Luecken mit Antworten fuellen
            for gap in gaps:
                word = gap.get("word", "")
                sentence = sentence.replace("___", f"<span class='gap-filled'>{word}</span>", 1)
        else:
            # Fragenblatt: Luecken als Linien
            sentence = sentence.replace("___", "<span class='gap'>&nbsp;</span>")
            # Woerter fuer Wortbank sammeln
            for gap in gaps:
                all_words.append(gap.get("word", ""))

        html_parts.append(f"<div class='cloze-sentence'>{sentence}</div>")

        # Uebersetzung anzeigen
        translation = item.get("translation", {})
        if translation:
            lang_name = translation.get("language_name", "Uebersetzung")
            full_sentence = translation.get("full_sentence", "")
            if full_sentence:
                html_parts.append("<div class='translation'>")
                html_parts.append(f"<div class='translation-label'>{lang_name}:</div>")
                html_parts.append(full_sentence)
                html_parts.append("</div>")

        html_parts.append("</div>")

    # Wortbank (nur fuer Fragenblatt)
    if not include_answers and all_words:
        random.shuffle(all_words)  # Mische die Woerter
        html_parts.append("<div class='word-bank'>")
        html_parts.append("<div class='word-bank-title'>Wortbank (diese Woerter fehlen):</div>")
        for word in all_words:
            html_parts.append(f"<span class='word'>{word}</span>")
        html_parts.append("</div>")

    html_parts.append("</body></html>")

    # Speichern
    suffix = "_cloze_solutions.html" if include_answers else "_cloze_print.html"
    out_name = cloze_path.stem.replace("_cloze", "") + suffix
    out_path = BEREINIGT_DIR / out_name
    out_path.write_text("\n".join(html_parts), encoding="utf-8")

    logger.info(f"Cloze Print-Version gespeichert: {out_path.name}")
    return out_path
