"""
AI Processor - Worksheet Export

Generate printable worksheet versions.
"""

from pathlib import Path
import json
import logging

logger = logging.getLogger(__name__)


def generate_print_version_worksheet(analysis_path: Path) -> str:
    """
    Generate a print-optimized HTML version of the worksheet.

    Features:
    - Large, readable font (16pt)
    - Black and white / grayscale compatible
    - Clear structure for printing
    - No interactive elements

    Args:
        analysis_path: Path to *_analyse.json file

    Returns:
        HTML string for direct delivery
    """
    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 enthaelt kein gueltiges 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(_get_worksheet_html_header(title))

    # Print button
    html_parts.append('<button class="print-button no-print" onclick="window.print()">🖨️ Drucken</button>')

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

    # Meta information
    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>")

    # Instructions
    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>")

    # Main text / printed blocks
    has_text_content = False
    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
            has_text_content = True
            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:
            has_text_content = True
            html_parts.append(f"<div class='text-block'>{p}</div>")
        html_parts.append("</section>")

    # Tasks
    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": "Lueckentext",
                "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>")

            # Answer lines for free text tasks
            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>")

    # Footer
    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)


def _get_worksheet_html_header(title: str) -> str:
    """Get HTML header for worksheet print version."""
    return f"""<!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 {{
    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>
"""
