#!/usr/bin/env python3
"""Generate Anki flashcards from exam questions in odpowiedzi/ folder.

Creates a tab-separated file compatible with Anki import.
"""

from __future__ import annotations

import logging
from pathlib import Path
import re

logger = logging.getLogger(__name__)

MIN_HEADER_WORDS = 3


def extract_main_question(content: str, filename: str) -> str:
    """Extract the main exam question from the file."""
    # Extract the main question from ## Pytanie section
    question_match = re.search(
        r'## Pytanie\s*\n\s*\*\*["\']?(.+?)["\']?\*\*', content, re.DOTALL
    )
    if question_match:
        main_question = question_match.group(1).strip()
        return re.sub(r"\s+", " ", main_question)

    # Fallback to title
    title_match = re.search(r"^# (.+)$", content, re.MULTILINE)
    return title_match.group(1) if title_match else filename


def extract_subject(content: str) -> str:
    """Extract the subject code."""
    subject_match = re.search(r"Przedmiot:\s*(\w+)", content)
    return subject_match.group(1) if subject_match else "Ogólne"


def extract_key_points(content: str) -> list[str]:
    """Extract key points from the main answer section."""
    points = []

    # Look for main answer section
    main_answer = re.search(
        r"## 📚 Odpowiedź główna\s*\n(.+?)(?=\n## [^�]|\n---\s*\n## |\Z)",
        content,
        re.DOTALL,
    )
    if not main_answer:
        return points

    answer_text = main_answer.group(1)

    # Extract ### headers as key points
    headers = re.findall(r"^### (.+)$", answer_text, re.MULTILINE)
    for h in headers[:6]:
        # Clean header
        cleaned = re.sub(r"\d+\.\s*", "", h).strip()
        if cleaned and len(cleaned) > MIN_HEADER_WORDS:
            points.append(cleaned)

    return points


def extract_definitions(content: str) -> list[tuple[str, str]]:
    """Extract key definitions from the content."""
    definitions = []

    # Pattern for **Term** - definition or **Term**: definition
    pattern = r"\*\*([^*\n]+)\*\*\s*[--:]\s*([^*\n]{20,150})"
    matches = re.findall(pattern, content)

    for raw_term, raw_def in matches:
        term = raw_term.strip()
        definition = raw_def.strip()
        # Filter out non-definition patterns
        if (
            term
            and definition
            and not term.startswith("Przykład")
            and not term.startswith("Uwaga")
        ):
            definitions.append((term, definition))

    return definitions[:5]


def clean_html(text: str) -> str:
    """Convert markdown to HTML and clean for Anki."""
    if not text:
        return ""

    # Replace markdown bold/italic with HTML
    text = re.sub(r"\*\*(.+?)\*\*", r"<b>\1</b>", text)
    text = re.sub(r"\*(.+?)\*", r"<i>\1</i>", text)

    # Clean up special characters
    text = text.replace("\t", " ")
    text = text.replace('"', "&quot;")

    # Handle newlines - convert to <br>
    text = text.replace("\n", " ")
    text = re.sub(r"\s+", " ", text)

    return text.strip()


def process_file(filepath: str) -> list[dict[str, str]]:
    """Process a single file and return flashcards."""
    with Path(filepath).open(encoding="utf-8") as f:
        content = f.read()

    cards = []

    # Extract metadata
    filename = Path(filepath).name
    match = re.match(r"(\d+)-(.+)\.md", filename)
    num = match.group(1) if match else "00"

    subject = extract_subject(content)
    main_question = extract_main_question(content, filename)

    # Base tags for this question
    base_tags = f"egzamin_magisterski pytanie_{num} {subject}"

    # Card 1: Main question with key points
    key_points = extract_key_points(content)
    if key_points:
        answer = (
            "<ul>"
            + "".join([f"<li>{clean_html(p)}</li>" for p in key_points])
            + "</ul>"
        )
        cards.append(
            {"front": clean_html(main_question), "back": answer, "tags": base_tags}
        )

    # Card 2+: Key definitions as individual cards
    definitions = extract_definitions(content)
    for term, definition in definitions:
        q = f"Definicja: {term}"
        a = clean_html(definition)
        cards.append({"front": q, "back": a, "tags": f"{base_tags} definicje"})

    return cards


def main() -> None:
    """Main."""
    odpowiedzi_dir = Path("/home/kuchy/praca_magisterska/pytania/odpowiedzi")
    output_file = Path(
        "/home/kuchy/praca_magisterska/pytania/anki_egzamin_magisterski.txt"
    )

    all_cards = []

    # Process each file
    for md_file in sorted(odpowiedzi_dir.glob("*.md")):
        logger.info("Processing: %s", md_file.name)
        try:
            cards = process_file(md_file)
            all_cards.extend(cards)
            logger.info("  -> %d cards", len(cards))
        except (ValueError, OSError):
            logger.exception("  -> Error processing file")

    # Write Anki-compatible file
    with Path(output_file).open("w", encoding="utf-8") as f:
        # File headers for Anki
        f.write("#separator:tab\n")
        f.write("#html:true\n")
        f.write("#tags column:3\n")
        f.write("#deck:Egzamin Magisterski ISY\n")
        f.write("#notetype:Basic\n")
        f.write("\n")

        for card in all_cards:
            front = card["front"]
            back = card["back"]
            tags = card["tags"]

            # Ensure no tabs in content
            front = front.replace("\t", " ")
            back = back.replace("\t", " ")

            f.write(f"{front}\t{back}\t{tags}\n")

    logger.info("Created %d flashcards", len(all_cards))
    logger.info("Output: %s", output_file)
    logger.info("=== Import Instructions ===")
    logger.info("1. Open Anki desktop -> File -> Import")
    logger.info("2. Select: anki_egzamin_magisterski.txt")
    logger.info("3. Set 'Fields separated by: Tab'")
    logger.info("4. Check 'Allow HTML in fields'")
    logger.info("5. Map: Field 1 -> Front, Field 2 -> Back, Field 3 -> Tags")
    logger.info("6. Click Import")
    logger.info("For AnkiWeb/AnkiDroid: Sync after importing on desktop")


if __name__ == "__main__":
    main()
