"""Golden-output tests for the PPTX scraper (Phase 2 port).

The golden trees under tests/golden/phase2/{pptx,pptx_kw}/ were captured from
the PRE-DocumentSkillBuilder code; these tests prove the port is
byte-identical. The fixture exercises every build path: presentation metadata
(title/author/subject/category/created/modified/slide_count), slide ranges,
h2/h3 sub-headings (incl. the h3-only "Slide Topics" branch), text with
trailing whitespace (the writer strips it), multi-language code samples
(incl. >500 chars + quality ordering), tables with and without headers,
image-count summaries, single-source AND keyword categorization paths,
presentation pattern keywords ("getting started"/"overview"/"summary"),
and language stats.
"""

import copy

from tests.phase2_golden_utils import assert_matches_golden, build_snapshot

LONG_CODE = "def long_example():\n" + "\n".join(f"    x{i} = {i}" for i in range(60))

SECTIONS = [
    {
        "section_number": 1,
        "heading": "Getting Started Guide",
        "heading_level": "h1",
        "headings": [
            {"level": "h2", "text": "Installation Steps"},
            {"level": "h3", "text": "Slide 2: Verify Setup"},
        ],
        "text": (
            "Welcome to the project. This section explains setup.\n\n"
            "### Speaker Notes\n\n[Slide 1] Remember to demo the install."
        ),
        "code_samples": [
            {"language": "python", "code": "print('hello')", "quality_score": 8.5},
            {"language": "bash", "code": "pip install thing", "quality_score": 6.0},
        ],
        "tables": [
            {"headers": ["Option", "Default"], "rows": [["debug", "false"], ["port", "8080"]]},
        ],
        "image_count": 2,
        "slide_range": "1-3",
        "slides": [],
    },
    {
        "section_number": 2,
        "heading": "Architecture Overview",
        "heading_level": "h2",
        "headings": [],
        # Trailing whitespace exercises the strip-before-write path
        "text": "All endpoints are documented here.\n",
        "code_samples": [
            {"language": "python", "code": LONG_CODE, "quality_score": 9.5},
        ],
        "tables": [
            # Table without headers exercises the headerless rendering path
            {"headers": [], "rows": [["a", "b"], ["c", "d"]]},
        ],
        "image_count": 0,
        "slide_range": "4-6",
        "slides": [],
    },
    {
        "section_number": 3,
        "heading": "Summary and Q&A",
        "heading_level": "h1",
        "headings": [{"level": "h3", "text": "Slide 8: Common Errors"}],
        "text": "",
        "code_samples": [],
        "tables": [],
        "image_count": 0,
        # No slide_range exercises the bare "Source: Section N" line
        "slide_range": "",
        "slides": [],
    },
]

# Variant with h3-only sub-headings (no h2 anywhere) — exercises the
# "Slide Topics" branch of _format_key_concepts.
SECTIONS_H3_ONLY = copy.deepcopy(SECTIONS)
SECTIONS_H3_ONLY[0]["headings"] = [
    {"level": "h3", "text": "Slide 2: Verify Setup"},
    {"level": "h3", "text": "Slide 3: First Demo"},
]
SECTIONS_H3_ONLY[1]["heading_level"] = "h1"


def _extracted_data(metadata: dict, sections=SECTIONS) -> dict:
    return {
        "source_file": "fixtures/deck.pptx",
        "metadata": metadata,
        "total_slides": 8,
        "total_sections": 3,
        "total_code_blocks": 3,
        "total_images": 2,
        "total_tables": 2,
        "languages_detected": {"python": 2, "bash": 1},
        "pages": copy.deepcopy(sections),
    }


def test_pptx_build_matches_golden(tmp_path):
    from skill_seekers.cli.pptx_scraper import PptxToSkillConverter

    converter = PptxToSkillConverter(
        {
            "name": "golden_pptx",
            "description": "Use when testing the pptx golden build",
            "pptx_path": "fixtures/deck.pptx",
            "output_dir": str(tmp_path / "skill"),
        }
    )
    converter.extracted_data = _extracted_data(
        {
            "title": "The Deck",
            "author": "Jane Doe",
            "subject": "Quarterly architecture review",
            "category": "Engineering",
            "created": "2024-01-01",
            "modified": "2024-03-03",
            "slide_count": 8,
        }
    )
    assert_matches_golden(build_snapshot(converter), "pptx")


def test_pptx_keyword_categorization_matches_golden(tmp_path):
    """No pptx_path → keyword categorization path (multi-source scenario)."""
    from skill_seekers.cli.pptx_scraper import PptxToSkillConverter

    converter = PptxToSkillConverter(
        {
            "name": "golden_pptx_kw",
            "description": "Use when testing keyword categorization",
            "output_dir": str(tmp_path / "skill"),
            "categories": {
                "setup": ["setup", "installation"],
                "api": ["endpoints"],
            },
        }
    )
    converter.extracted_data = _extracted_data({}, sections=SECTIONS_H3_ONLY)
    assert_matches_golden(build_snapshot(converter), "pptx_kw")
