"""
AI Processing - Print Version Generator.

Generiert druckbare HTML-Versionen für verschiedene Arbeitsblatt-Typen.
"""

from pathlib import Path
import json
import random
import logging

from .core import BEREINIGT_DIR

logger = logging.getLogger(__name__)


def generate_print_version_qa(qa_path: Path, include_answers: bool = False) -> Path:
    """
    Generiert eine druckbare HTML-Version der Frage-Antwort-Paare.

    Args:
        qa_path: Pfad zur *_qa.json Datei
        include_answers: True für Lösungsblatt (für Eltern)

    Returns:
        Pfad zur generierten HTML-Datei
    """
    if not qa_path.exists():
        raise FileNotFoundError(f"Q&A-Datei nicht gefunden: {qa_path}")

    qa_data = json.loads(qa_path.read_text(encoding="utf-8"))
    items = qa_data.get("qa_items", [])
    metadata = qa_data.get("metadata", {})

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

    html_parts = []
    html_parts.append("""<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>""" + title + """ - Fragen</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.6;
  }
  h1 { font-size: 24px; margin-bottom: 8px; }
  .meta { color: #666; margin-bottom: 24px; }
  .question-block {
    margin-bottom: 32px;
    padding-bottom: 16px;
    border-bottom: 1px dashed #ccc;
  }
  .question-number {
    font-weight: bold;
    color: #333;
  }
  .question-text {
    font-size: 16px;
    margin: 8px 0;
  }
  .answer-space {
    border: 1px solid #ddd;
    min-height: 60px;
    margin-top: 12px;
    background: #fafafa;
  }
  .answer-lines {
    margin-top: 12px;
  }
  .answer-line {
    border-bottom: 1px solid #999;
    height: 28px;
  }
  .answer {
    margin-top: 8px;
    padding: 8px;
    background: #e8f5e9;
    border-left: 3px solid #4caf50;
  }
  .key-terms {
    font-size: 12px;
    color: #666;
    margin-top: 8px;
  }
  .key-terms span {
    background: #fff3e0;
    padding: 2px 6px;
    border-radius: 3px;
    margin-right: 4px;
  }
</style>
</head>
<body>
""")

    # Header
    version_text = "Lösungsblatt" if include_answers else "Fragenblatt"
    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"Anzahl Fragen: {len(items)}")
    html_parts.append(f"<div class='meta'>{' | '.join(meta_parts)}</div>")

    # Fragen
    for idx, item in enumerate(items, 1):
        html_parts.append("<div class='question-block'>")
        html_parts.append(f"<div class='question-number'>Frage {idx}</div>")
        html_parts.append(f"<div class='question-text'>{item.get('question', '')}</div>")

        if include_answers:
            # Lösungsblatt: Antwort anzeigen
            html_parts.append(f"<div class='answer'><strong>Antwort:</strong> {item.get('answer', '')}</div>")
            # Schlüsselbegriffe
            key_terms = item.get("key_terms", [])
            if key_terms:
                terms_html = " ".join([f"<span>{term}</span>" for term in key_terms])
                html_parts.append(f"<div class='key-terms'>Wichtige Begriffe: {terms_html}</div>")
        else:
            # Fragenblatt: Antwortlinien
            html_parts.append("<div class='answer-lines'>")
            for _ in range(3):
                html_parts.append("<div class='answer-line'></div>")
            html_parts.append("</div>")

        html_parts.append("</div>")

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

    # Speichern
    suffix = "_qa_solutions.html" if include_answers else "_qa_print.html"
    out_name = qa_path.stem.replace("_qa", "") + suffix
    out_path = BEREINIGT_DIR / out_name
    out_path.write_text("\n".join(html_parts), encoding="utf-8")

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


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

    Args:
        cloze_path: Pfad zur *_cloze.json Datei
        include_answers: True für Lösungsblatt (für 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 + """ - Lückentext</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 = "Lösungsblatt" if include_answers else "Lückentext"
    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"Lücken gesamt: {total_gaps}")
    html_parts.append(f"<div class='meta'>{' | '.join(meta_parts)}</div>")

    # Sammle alle Lückenwörter für Wortbank
    all_words = []

    # Lückentexte
    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:
            # Lösungsblatt: Lücken mit Antworten füllen
            for gap in gaps:
                word = gap.get("word", "")
                sentence = sentence.replace("___", f"<span class='gap-filled'>{word}</span>", 1)
        else:
            # Fragenblatt: Lücken als Linien
            sentence = sentence.replace("___", "<span class='gap'>&nbsp;</span>")
            # Wörter für Wortbank sammeln
            for gap in gaps:
                all_words.append(gap.get("word", ""))

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

        # Übersetzung anzeigen
        translation = item.get("translation", {})
        if translation:
            lang_name = translation.get("language_name", "Übersetzung")
            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 für Fragenblatt)
    if not include_answers and all_words:
        random.shuffle(all_words)  # Mische die Wörter
        html_parts.append("<div class='word-bank'>")
        html_parts.append("<div class='word-bank-title'>Wortbank (diese Wörter 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


def generate_print_version_mc(mc_path: Path, include_answers: bool = False) -> str:
    """
    Generiert eine druckbare HTML-Version der Multiple-Choice-Fragen.

    Args:
        mc_path: Pfad zur *_mc.json Datei
        include_answers: True für Lösungsblatt mit markierten richtigen Antworten

    Returns:
        HTML-String (zum direkten Ausliefern)
    """
    if not mc_path.exists():
        raise FileNotFoundError(f"MC-Datei nicht gefunden: {mc_path}")

    mc_data = json.loads(mc_path.read_text(encoding="utf-8"))
    questions = mc_data.get("questions", [])
    metadata = mc_data.get("metadata", {})

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

    html_parts = []
    html_parts.append("""<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>""" + title + """ - Multiple Choice</title>
<style>
  @media print {
    .no-print { display: none; }
    .page-break { page-break-before: always; }
    body { font-size: 14pt; }
  }
  body {
    font-family: Arial, Helvetica, sans-serif;
    max-width: 800px;
    margin: 40px auto;
    padding: 20px;
    line-height: 1.6;
    color: #000;
  }
  h1 {
    font-size: 28px;
    margin-bottom: 8px;
    border-bottom: 2px solid #000;
    padding-bottom: 8px;
  }
  .meta {
    color: #333;
    margin-bottom: 32px;
    font-size: 14px;
  }
  .instructions {
    background: #f5f5f5;
    padding: 12px 16px;
    border-radius: 4px;
    margin-bottom: 24px;
    font-size: 14px;
  }
  .question-block {
    margin-bottom: 28px;
    padding-bottom: 16px;
    border-bottom: 1px solid #ddd;
  }
  .question-number {
    font-weight: bold;
    font-size: 18px;
    color: #000;
    margin-bottom: 8px;
  }
  .question-text {
    font-size: 16px;
    margin: 8px 0 16px 0;
    line-height: 1.5;
  }
  .options {
    margin-left: 20px;
  }
  .option {
    display: flex;
    align-items: flex-start;
    margin-bottom: 12px;
    padding: 8px 12px;
    border: 1px solid #ccc;
    border-radius: 4px;
    background: #fff;
  }
  .option-correct {
    background: #e8f5e9;
    border-color: #4caf50;
    border-width: 2px;
  }
  .option-checkbox {
    width: 20px;
    height: 20px;
    border: 2px solid #333;
    border-radius: 50%;
    margin-right: 12px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .option-checkbox.checked::after {
    content: "✓";
    font-weight: bold;
    color: #4caf50;
  }
  .option-label {
    font-weight: bold;
    margin-right: 8px;
    min-width: 24px;
  }
  .option-text {
    flex: 1;
  }
  .explanation {
    margin-top: 8px;
    padding: 8px 12px;
    background: #e3f2fd;
    border-left: 3px solid #2196f3;
    font-size: 13px;
    color: #333;
  }
  .answer-key {
    margin-top: 40px;
    padding: 16px;
    background: #f5f5f5;
    border-radius: 8px;
  }
  .answer-key-title {
    font-weight: bold;
    font-size: 18px;
    margin-bottom: 12px;
    border-bottom: 1px solid #999;
    padding-bottom: 8px;
  }
  .answer-key-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 8px;
  }
  .answer-key-item {
    padding: 8px;
    text-align: center;
    background: white;
    border: 1px solid #ddd;
    border-radius: 4px;
  }
  .answer-key-q {
    font-weight: bold;
  }
  .answer-key-a {
    color: #4caf50;
    font-weight: bold;
  }
</style>
</head>
<body>
""")

    # Header
    version_text = "Lösungsblatt" if include_answers else "Multiple Choice Test"
    html_parts.append(f"<h1>{title}</h1>")
    html_parts.append(f"<div class='meta'><strong>{version_text}</strong>")
    if subject:
        html_parts.append(f" | Fach: {subject}")
    if grade:
        html_parts.append(f" | Klasse: {grade}")
    html_parts.append(f" | Anzahl Fragen: {len(questions)}</div>")

    if not include_answers:
        html_parts.append("<div class='instructions'>")
        html_parts.append("<strong>Anleitung:</strong> Kreuze bei jeder Frage die richtige Antwort an. ")
        html_parts.append("Es ist immer nur eine Antwort richtig.")
        html_parts.append("</div>")

    # Fragen
    for idx, q in enumerate(questions, 1):
        html_parts.append("<div class='question-block'>")
        html_parts.append(f"<div class='question-number'>Frage {idx}</div>")
        html_parts.append(f"<div class='question-text'>{q.get('question', '')}</div>")

        html_parts.append("<div class='options'>")
        correct_answer = q.get("correct_answer", "")

        for opt in q.get("options", []):
            opt_id = opt.get("id", "")
            is_correct = opt_id == correct_answer

            opt_class = "option"
            checkbox_class = "option-checkbox"
            if include_answers and is_correct:
                opt_class += " option-correct"
                checkbox_class += " checked"

            html_parts.append(f"<div class='{opt_class}'>")
            html_parts.append(f"<div class='{checkbox_class}'></div>")
            html_parts.append(f"<span class='option-label'>{opt_id})</span>")
            html_parts.append(f"<span class='option-text'>{opt.get('text', '')}</span>")
            html_parts.append("</div>")

        html_parts.append("</div>")

        # Erklärung nur bei Lösungsblatt
        if include_answers and q.get("explanation"):
            html_parts.append(f"<div class='explanation'><strong>Erklärung:</strong> {q.get('explanation')}</div>")

        html_parts.append("</div>")

    # Lösungsschlüssel (kompakt) - nur bei Lösungsblatt
    if include_answers:
        html_parts.append("<div class='answer-key'>")
        html_parts.append("<div class='answer-key-title'>Lösungsschlüssel</div>")
        html_parts.append("<div class='answer-key-grid'>")
        for idx, q in enumerate(questions, 1):
            html_parts.append("<div class='answer-key-item'>")
            html_parts.append(f"<span class='answer-key-q'>{idx}.</span> ")
            html_parts.append(f"<span class='answer-key-a'>{q.get('correct_answer', '')}</span>")
            html_parts.append("</div>")
        html_parts.append("</div>")
        html_parts.append("</div>")

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

    return "\n".join(html_parts)


def generate_print_version_worksheet(analysis_path: Path) -> str:
    """
    Generiert eine druckoptimierte HTML-Version des Arbeitsblatts.

    Eigenschaften:
    - Große, gut lesbare Schrift (16pt)
    - Schwarz-weiß / Graustufen-tauglich
    - Klare Struktur für Druck
    - Keine interaktiven Elemente

    Args:
        analysis_path: Pfad zur *_analyse.json Datei

    Returns:
        HTML-String zum direkten Ausliefern
    """
    if not analysis_path.exists():
        raise FileNotFoundError(f"Analysedatei nicht gefunden: {analysis_path}")

    try:
        data = json.loads(analysis_path.read_text(encoding="utf-8"))
    except json.JSONDecodeError as e:
        raise RuntimeError(f"Analyse-Datei enthält kein gültiges JSON: {analysis_path}\n{e}") from e

    title = data.get("title") or "Arbeitsblatt"
    subject = data.get("subject") or ""
    grade_level = data.get("grade_level") or ""
    instructions = data.get("instructions") or ""
    tasks = data.get("tasks", []) or []
    canonical_text = data.get("canonical_text") or ""
    printed_blocks = data.get("printed_blocks") or []

    html_parts = []
    html_parts.append("""<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>""" + title + """</title>
<style>
  @page {
    size: A4;
    margin: 20mm;
  }
  @media print {
    body {
      font-size: 14pt !important;
      -webkit-print-color-adjust: exact;
      print-color-adjust: exact;
    }
    .no-print { display: none !important; }
    .page-break { page-break-before: always; }
  }
  * { box-sizing: border-box; }
  body {
    font-family: Arial, "Helvetica Neue", sans-serif;
    max-width: 800px;
    margin: 0 auto;
    padding: 30px;
    line-height: 1.7;
    font-size: 16px;
    color: #000;
    background: #fff;
  }
  h1 {
    font-size: 28px;
    margin: 0 0 8px 0;
    padding-bottom: 8px;
    border-bottom: 3px solid #000;
  }
  h2 {
    font-size: 20px;
    margin: 28px 0 12px 0;
    padding-bottom: 4px;
    border-bottom: 1px solid #666;
  }
  .meta {
    font-size: 14px;
    color: #333;
    margin-bottom: 20px;
    padding: 8px 0;
  }
  .meta span {
    margin-right: 20px;
  }
  .instructions {
    margin: 20px 0;
    padding: 16px;
    border: 2px solid #333;
    background: #f5f5f5;
    font-size: 15px;
  }
  .instructions-label {
    font-weight: bold;
    margin-bottom: 8px;
  }
  .text-section {
    margin: 24px 0;
  }
  .text-block {
    margin-bottom: 16px;
    text-align: justify;
  }
  .text-block-title {
    font-weight: bold;
    font-size: 17px;
    margin-bottom: 8px;
  }
  .task-section {
    margin-top: 32px;
  }
  .task {
    margin-bottom: 24px;
    padding: 16px;
    border: 1px solid #999;
    background: #fafafa;
  }
  .task-header {
    font-weight: bold;
    font-size: 16px;
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 1px dashed #666;
  }
  .task-content {
    font-size: 15px;
  }
  .gap-line {
    display: inline-block;
    border-bottom: 2px solid #000;
    min-width: 100px;
    margin: 0 6px;
  }
  .answer-lines {
    margin-top: 16px;
  }
  .answer-line {
    border-bottom: 1px solid #333;
    height: 36px;
    margin-bottom: 4px;
  }
  .footer {
    margin-top: 40px;
    padding-top: 16px;
    border-top: 1px solid #ccc;
    font-size: 11px;
    color: #666;
    text-align: center;
  }
  /* Print Button - versteckt beim Drucken */
  .print-button {
    position: fixed;
    top: 20px;
    right: 20px;
    padding: 12px 24px;
    background: #333;
    color: #fff;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
  }
  .print-button:hover {
    background: #555;
  }
</style>
</head>
<body>
<button class="print-button no-print" onclick="window.print()">🖨️ Drucken</button>
""")

    # Titel
    html_parts.append(f"<h1>{title}</h1>")

    # Meta-Informationen
    meta_parts = []
    if subject:
        meta_parts.append(f"<span><strong>Fach:</strong> {subject}</span>")
    if grade_level:
        meta_parts.append(f"<span><strong>Klasse:</strong> {grade_level}</span>")
    if meta_parts:
        html_parts.append(f"<div class='meta'>{''.join(meta_parts)}</div>")

    # Arbeitsanweisung
    if instructions:
        html_parts.append("<div class='instructions'>")
        html_parts.append("<div class='instructions-label'>Arbeitsanweisung:</div>")
        html_parts.append(f"<div>{instructions}</div>")
        html_parts.append("</div>")

    # Haupttext / gedruckte Blöcke
    if printed_blocks:
        html_parts.append("<section class='text-section'>")
        for block in printed_blocks:
            role = (block.get("role") or "body").lower()
            text = (block.get("text") or "").strip()
            if not text:
                continue
            if role == "title":
                html_parts.append(f"<div class='text-block'><div class='text-block-title'>{text}</div></div>")
            else:
                html_parts.append(f"<div class='text-block'>{text}</div>")
        html_parts.append("</section>")
    elif canonical_text:
        html_parts.append("<section class='text-section'>")
        paragraphs = [
            p.strip()
            for p in canonical_text.replace("\r\n", "\n").split("\n\n")
            if p.strip()
        ]
        for p in paragraphs:
            html_parts.append(f"<div class='text-block'>{p}</div>")
        html_parts.append("</section>")

    # Aufgaben
    if tasks:
        html_parts.append("<section class='task-section'>")
        html_parts.append("<h2>Aufgaben</h2>")

        for idx, task in enumerate(tasks, start=1):
            t_type = task.get("type") or "Aufgabe"
            desc = task.get("description") or ""
            text_with_gaps = task.get("text_with_gaps")

            html_parts.append("<div class='task'>")

            # Task-Header
            type_label = {
                "fill_in_blank": "Lückentext",
                "multiple_choice": "Multiple Choice",
                "free_text": "Freitext",
                "matching": "Zuordnung",
                "labeling": "Beschriftung",
                "calculation": "Rechnung",
                "other": "Aufgabe"
            }.get(t_type, t_type)

            html_parts.append(f"<div class='task-header'>Aufgabe {idx}: {type_label}</div>")

            if desc:
                html_parts.append(f"<div class='task-content'>{desc}</div>")

            if text_with_gaps:
                rendered = text_with_gaps.replace("___", "<span class='gap-line'>&nbsp;</span>")
                html_parts.append(f"<div class='task-content' style='margin-top:12px;'>{rendered}</div>")

            # Antwortlinien für Freitext-Aufgaben
            if t_type in ["free_text", "other"] or (not text_with_gaps and not desc):
                html_parts.append("<div class='answer-lines'>")
                for _ in range(3):
                    html_parts.append("<div class='answer-line'></div>")
                html_parts.append("</div>")

            html_parts.append("</div>")

        html_parts.append("</section>")

    # Fußzeile
    html_parts.append("<div class='footer'>")
    html_parts.append("Dieses Arbeitsblatt wurde automatisch aus einem Scan rekonstruiert.")
    html_parts.append("</div>")

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

    return "\n".join(html_parts)
