"""TDD tests for compiler/stages/normalize.py — known input → known output."""

from __future__ import annotations

from pathlib import Path

from compiler.stages.normalize import _strip_gutenberg, normalize_all, normalize_source

FIXTURES_HTTP = Path(__file__).parent.parent / "fixtures" / "http"

GUTENBERG_SRC = {
    "id": "meditations-gutenberg",
    "fetch_type": "gutenberg",
    "gutenberg_id": 2680,
    "title": "Meditations (George Long translation)",
    "rights": "public-domain",
}

WIKIPEDIA_SRC = {
    "id": "wikipedia-marcus-aurelius",
    "fetch_type": "wikipedia",
    "wikipedia_title": "Marcus Aurelius",
    "title": "Marcus Aurelius — Wikipedia",
    "rights": "public-domain",
}

URL_SRC = {
    "id": "wikiquote-marcus-aurelius",
    "fetch_type": "url",
    "url": "https://en.wikiquote.org/wiki/Marcus_Aurelius",
    "title": "Marcus Aurelius — Wikiquote",
    "rights": "public-domain",
}

TRANSCRIPT_SRC = {
    "id": "interview-transcript",
    "fetch_type": "youtube",
    "url": "https://example.com/watch?v=abc",
    "title": "Interview Transcript",
    "rights": "personal-use",
}


# ── _strip_gutenberg ─────────────────────────────────────────────────────────


def test_strip_gutenberg_removes_header_footer():
    raw = (FIXTURES_HTTP / "gutenberg_2680.txt").read_text()
    body = _strip_gutenberg(raw)
    assert "Project Gutenberg" not in body
    assert "Meditations" in body


def test_strip_gutenberg_preserves_body_text():
    raw = (FIXTURES_HTTP / "gutenberg_2680.txt").read_text()
    body = _strip_gutenberg(raw)
    assert "gentle and meek" in body
    assert "transformation" in body


# ── normalize_source ─────────────────────────────────────────────────────────


def test_normalize_gutenberg_has_frontmatter(tmp_path):
    raw = FIXTURES_HTTP / "gutenberg_2680.txt"
    dest = normalize_source(raw, GUTENBERG_SRC, tmp_path / "corpus")
    content = dest.read_text()
    assert content.startswith("---")
    assert "source_id: meditations-gutenberg" in content
    assert "rights: public-domain" in content
    assert "origin_url: https://www.gutenberg.org/ebooks/2680" in content


def test_normalize_gutenberg_no_boilerplate(tmp_path):
    raw = FIXTURES_HTTP / "gutenberg_2680.txt"
    dest = normalize_source(raw, GUTENBERG_SRC, tmp_path / "corpus")
    content = dest.read_text()
    assert "Project Gutenberg" not in content.split("---", 2)[-1]


def test_normalize_wikipedia(tmp_path):
    raw = FIXTURES_HTTP / "wikipedia_marcus_aurelius.json"
    dest = normalize_source(raw, WIKIPEDIA_SRC, tmp_path / "corpus")
    content = dest.read_text()
    assert "source_id: wikipedia-marcus-aurelius" in content
    assert "Stoic" in content


def test_normalize_url(tmp_path):
    raw = FIXTURES_HTTP / "wikiquote_marcus_aurelius.html"
    dest = normalize_source(raw, URL_SRC, tmp_path / "corpus")
    content = dest.read_text()
    assert "source_id: wikiquote-marcus-aurelius" in content
    assert len(content) > 100


def test_normalize_transcript(tmp_path):
    raw = tmp_path / "interview-transcript.txt"
    raw.write_text("[00:00:00.000 --> 00:00:01.000] SPEAKER_00: Hello.\n")

    dest = normalize_source(raw, TRANSCRIPT_SRC, tmp_path / "corpus")

    content = dest.read_text()
    assert "source_id: interview-transcript" in content
    assert "fetch_type: youtube" in content
    assert "origin_url: https://example.com/watch?v=abc" in content
    assert "SPEAKER_00: Hello." in content


def test_normalize_output_is_valid_md(tmp_path):
    """Resulting file must start with frontmatter and have a title heading."""
    raw = FIXTURES_HTTP / "gutenberg_2680.txt"
    dest = normalize_source(raw, GUTENBERG_SRC, tmp_path / "corpus")
    content = dest.read_text()
    parts = content.split("---", 2)
    assert len(parts) >= 3, "Expected frontmatter delimiters"
    body_after_fm = parts[2]
    assert "# " in body_after_fm


# ── normalize_all ─────────────────────────────────────────────────────────────


def test_normalize_all(tmp_path):
    raw_paths = {
        "meditations-gutenberg": FIXTURES_HTTP / "gutenberg_2680.txt",
        "wikipedia-marcus-aurelius": FIXTURES_HTTP / "wikipedia_marcus_aurelius.json",
    }
    sources = [GUTENBERG_SRC, WIKIPEDIA_SRC]
    results = normalize_all(raw_paths, sources, tmp_path / "corpus")
    assert "meditations-gutenberg" in results
    assert "wikipedia-marcus-aurelius" in results
    for path in results.values():
        assert path.exists()
        assert path.suffix == ".md"
