"""
AI Processing - Print Version Generator: Multiple Choice.

Generates printable HTML for multiple-choice worksheets.
"""

from pathlib import Path
import json
import logging

logger = logging.getLogger(__name__)


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 fuer Loesungsblatt 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: "\u2713";
    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 = "Loesungsblatt" 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>")

        # Erklaerung nur bei Loesungsblatt
        if include_answers and q.get("explanation"):
            html_parts.append(f"<div class='explanation'><strong>Erklaerung:</strong> {q.get('explanation')}</div>")

        html_parts.append("</div>")

    # Loesungsschluessel (kompakt) - nur bei Loesungsblatt
    if include_answers:
        html_parts.append("<div class='answer-key'>")
        html_parts.append("<div class='answer-key-title'>Loesungsschluessel</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)
