"""
build_course_map.py
Lee course_content.json y extrae de los filenames de S3:
  - Titulo real de cada leccion (del nombre del audio)
  - Capitulo y numero de historia
  - Titulo de seccion correcto

Genera course_map.json listo para importar a la DB.
"""
import json
import re
import sys
import urllib.parse
from pathlib import Path

sys.stdout.reconfigure(encoding="utf-8")

SCRIPTS_DIR   = Path(__file__).parent
CONTENT_FILE  = SCRIPTS_DIR / "course_content.json"
MAP_FILE      = SCRIPTS_DIR / "course_map.json"

# Lecciones especiales conocidas (de la DB actual)
KNOWN_LESSONS = {
    1: {"title": "Introducción a RAIO",             "section": "Comienza aquí",   "section_order": 1},
    2: {"title": "PORTUGUÊS - Perguntas Frecuentes", "section": "Comienza aquí",   "section_order": 1},
}

def parse_s3_audio_name(url: str) -> dict | None:
    """
    Extrae chapter, story y topic del filename S3.
    Patron principal: Chapter X - Story Y - Topic Name - Normal/Interactive version.mp3
    Patrones alternativos para Chapter 3 y bonus stories.
    """
    try:
        path = urllib.parse.unquote_plus(urllib.parse.urlparse(url).path)
        stem = Path(path).stem  # sin extension

        # 1) Patron principal: "Chapter 1 - Story 1 - Presenting yourself - Normal version"
        m = re.match(
            r'Chapter\s+(\d+)\s*[-–]\s*Story\s+(\d+)\s*[-–]\s*(.+?)\s*[-–]\s*(Normal|Interactive)',
            stem, re.I
        )
        if m:
            return {
                "chapter": int(m.group(1)),
                "story":   int(m.group(2)),
                "topic":   m.group(3).strip().title(),
            }

        # 2) Chapter 3 sin dash antes de Normal/Interactive:
        #    "Chapter 3 - Story 9 - Culture Normal" / "Chapter 3 - Story 1 - Presenting Yourself"
        m2 = re.match(
            r'Chapter\s+(\d+)\s*[-–]\s*Story\s+(\d+)\s*[-–]\s*(.+)',
            stem, re.I
        )
        if m2:
            topic_raw = m2.group(3).strip()
            # Strip trailing Normal/Interactive/mp3/version noise
            topic_clean = re.sub(
                r'\s*[-–]?\s*(Normal|Interactive|mp3|version)\s*\.?\s*$',
                '', topic_raw, flags=re.I
            ).strip()
            if topic_clean:
                return {
                    "chapter": int(m2.group(1)),
                    "story":   int(m2.group(2)),
                    "topic":   topic_clean.title(),
                }

        # 3) Bonus stories: "funny-chapter1-normal", "drama-chapter2-interactive"
        m3 = re.match(
            r'^(funny|drama|motivation|psycho|dates)-chapter(\d+)-(normal|interactive)',
            stem, re.I
        )
        if m3:
            genre_map = {
                "funny":      "Funny Story",
                "drama":      "Drama Story",
                "motivation": "Motivation Story",
                "psycho":     "Psychology Story",
                "dates":      "Dates Story",
            }
            genre = genre_map.get(m3.group(1).lower(), m3.group(1).title())
            return {
                "chapter": int(m3.group(2)),
                "story":   None,
                "topic":   genre,
            }

        # 4) Sin "Story N": "Chapter 2 - Topic - Normal"
        m4 = re.match(
            r'Chapter\s+(\d+)\s*[-–]\s*(.+?)\s*[-–]\s*(Normal|Interactive)',
            stem, re.I
        )
        if m4:
            return {
                "chapter": int(m4.group(1)),
                "story":   None,
                "topic":   m4.group(2).strip().title(),
            }
    except Exception:
        pass
    return None


def section_title(chapter: int) -> str:
    labels = {
        1: "Capítulo 1: Historias Interactivas [Yes/No]",
        2: "Capítulo 2: Historias Interactivas [Open-Ended]",
        3: "Capítulo 3: Conversaciones Avanzadas",
        4: "Capítulo 4: Situaciones Reales",
        5: "Capítulo 5: Fluencia y Práctica",
    }
    return labels.get(chapter, f"Capítulo {chapter}")


def main():
    if not CONTENT_FILE.exists():
        print("No encontre course_content.json. Ejecuta primero scrape_content.py")
        sys.exit(1)

    lessons = json.loads(CONTENT_FILE.read_text(encoding="utf-8"))
    print(f"Procesando {len(lessons)} lecciones...")

    mapped = []
    unknown_titles = 0

    for lesson in lessons:
        num = lesson["num"]
        audio_urls = lesson.get("audio_urls", [])
        doc_urls   = lesson.get("doc_urls",   [])

        # Usar leccion conocida si existe
        if num in KNOWN_LESSONS:
            info = KNOWN_LESSONS[num]
            mapped.append({
                "num":           num,
                "title":         info["title"],
                "section_title": info["section"],
                "section_order": info["section_order"],
                "chapter":       None,
                "story":         None,
                "vidalytics_id": lesson.get("vidalytics_id"),
                "stream_url":    lesson.get("stream_url"),
                "audio_urls":    audio_urls,
                "doc_urls":      doc_urls,
                "notes":         lesson.get("notes", ""),
                "subtitle_en":   lesson.get("subtitle_en"),
                "subtitle_es":   lesson.get("subtitle_es"),
            })
            continue

        # Intentar extraer titulo del filename del audio
        parsed = None
        for audio in audio_urls:
            url = audio.get("url", "")
            if "normal" in url.lower():  # prioritize "Normal version"
                parsed = parse_s3_audio_name(url)
                if parsed:
                    break
        if not parsed:
            for audio in audio_urls:
                parsed = parse_s3_audio_name(audio.get("url", ""))
                if parsed:
                    break

        if parsed:
            title         = parsed["topic"]
            chapter       = parsed["chapter"]
            story         = parsed["story"]
            sect_title    = section_title(chapter)
            sect_order    = chapter + 1  # Comienza aqui = order 1, Cap1 = 2, etc.
        else:
            # Sin audio — lecciones de reglas, bonus, etc.
            raw = lesson.get("title", "")
            bad = {"importante", "working...", "working", "loading"}
            title   = raw if raw.lower() not in bad else f"Lección {num}"
            chapter = None
            story   = None
            # Asignar seccion por posicion numerica cuando no hay audio
            if num <= 2:
                sect_title = "Comienza aquí"
                sect_order = 1
            elif num <= 37:
                sect_title = section_title(1)
                sect_order = 2
                chapter    = 1
            elif num <= 72:
                sect_title = section_title(2)
                sect_order = 3
                chapter    = 2
            elif num <= 95:
                sect_title = section_title(3)
                sect_order = 4
                chapter    = 3
            else:
                sect_title = "Contenido Extra"
                sect_order = 99
            unknown_titles += 1

        mapped.append({
            "num":           num,
            "title":         title,
            "section_title": sect_title,
            "section_order": sect_order,
            "chapter":       chapter,
            "story":         story,
            "vidalytics_id": lesson.get("vidalytics_id"),
            "stream_url":    lesson.get("stream_url"),
            "audio_urls":    audio_urls,
            "doc_urls":      doc_urls,
            "notes":         lesson.get("notes", ""),
            "subtitle_en":   lesson.get("subtitle_en"),
            "subtitle_es":   lesson.get("subtitle_es"),
        })

    MAP_FILE.write_text(
        json.dumps(mapped, ensure_ascii=False, indent=2),
        encoding="utf-8"
    )

    # Estadisticas
    chapters = {}
    for l in mapped:
        c = l["chapter"] or 0
        chapters[c] = chapters.get(c, 0) + 1

    print(f"\nResultado -> {MAP_FILE.name}")
    for c in sorted(chapters):
        label = "Intro/Especiales" if c == 0 else f"Capítulo {c}"
        print(f"  {label}: {chapters[c]} lecciones")
    print(f"\nTitulos sin audio: {unknown_titles}")
    print(f"Total: {len(mapped)} lecciones")


if __name__ == "__main__":
    main()
