"""Build verification tests for the GodotIQ wheel.

These tests inspect the contents of the built wheel to confirm
all expected package data and modules are included.

Requires: a wheel already built in dist/ (run `python -m build` first).
"""

import zipfile
from pathlib import Path

import pytest

DIST_DIR = Path(__file__).resolve().parent.parent / "dist"


def _find_wheel() -> Path:
    """Find the most recent .whl file in dist/."""
    wheels = sorted(DIST_DIR.glob("godotiq-*.whl"))
    if not wheels:
        pytest.skip("No wheel found in dist/. Run `python -m build` first.")
    return wheels[-1]


def _wheel_namelist() -> list[str]:
    """Return the list of file paths inside the wheel."""
    whl = _find_wheel()
    with zipfile.ZipFile(whl) as zf:
        return zf.namelist()


class TestWheelContainsAddonFiles:
    """Wheel must include addon package data from force-include."""

    def test_wheel_contains_plugin_cfg(self) -> None:
        names = _wheel_namelist()
        assert any(n.endswith("addon/plugin.cfg") for n in names)

    def test_wheel_contains_gd_files(self) -> None:
        names = _wheel_namelist()
        gd_files = [n for n in names if "addon/" in n and n.endswith(".gd")]
        assert len(gd_files) >= 5, f"Expected >=5 .gd files in addon/, got {gd_files}"


class TestWheelContainsPrompts:
    """Wheel must include prompt markdown files."""

    def test_wheel_contains_godot_development_md(self) -> None:
        names = _wheel_namelist()
        assert any("prompts/godot_development.md" in n for n in names)


class TestWheelExclusions:
    """Wheel must NOT contain test or doc files."""

    def test_wheel_excludes_tests_directory(self) -> None:
        names = _wheel_namelist()
        test_files = [n for n in names if n.startswith("tests/")]
        assert test_files == [], f"Unexpected test files in wheel: {test_files}"

    def test_wheel_excludes_docs_directory(self) -> None:
        names = _wheel_namelist()
        doc_files = [n for n in names if n.startswith("docs/")]
        assert doc_files == [], f"Unexpected doc files in wheel: {doc_files}"


class TestInstalledPackage:
    """Tests that verify the installed package works correctly."""

    def test_version_importable(self) -> None:
        """__version__ is importable from the top-level package."""
        from godotiq import __version__
        assert isinstance(__version__, str)
        assert __version__ != "0.0.0-dev"

    def test_addon_package_importable(self) -> None:
        """godotiq.addon is importable and its directory exists."""
        import godotiq.addon
        addon_dir = Path(godotiq.addon.__file__).parent
        assert addon_dir.is_dir()

    def test_addon_has_gd_files(self) -> None:
        """Addon directory contains at least 5 .gd files (wheel install only)."""
        import godotiq.addon
        addon_dir = Path(godotiq.addon.__file__).parent
        gd_files = list(addon_dir.glob("*.gd"))
        if len(gd_files) < 5:
            pytest.skip("Addon .gd files not present in editable install")
        assert len(gd_files) >= 5
