#!/usr/bin/env python3
"""Check that all version metadata sources agree with the latest git tag on HEAD."""
import re
import subprocess
import sys
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parent.parent


def get_head_tag() -> tuple[str | None, bool]:
    """Return (tag, exact) — the tag on or closest to HEAD."""
    try:
        # Try exact match first.
        result = subprocess.run(
            ["git", "describe", "--tags", "--exact-match", "HEAD"],
            capture_output=True,
            text=True,
            cwd=REPO_ROOT,
        )
        if result.returncode == 0:
            return result.stdout.strip(), True
        # Fall back to closest ancestor tag.
        result = subprocess.run(
            ["git", "describe", "--tags", "--abbrev=0"],
            capture_output=True,
            text=True,
            cwd=REPO_ROOT,
        )
        if result.returncode == 0:
            return result.stdout.strip(), False
    except FileNotFoundError:
        pass
    return None, False


def strip_v(tag: str) -> str:
    return tag.lstrip("v")


def read_pyproject_version() -> str | None:
    p = REPO_ROOT / "pyproject.toml"
    if not p.exists():
        return None
    for line in p.read_text(encoding="utf-8").splitlines():
        m = re.match(r'^version\s*=\s*"([^"]+)"', line)
        if m:
            return m.group(1)
    return None


def read_init_version() -> tuple[str | None, str | None]:
    """Return (__version__ value, file path) from src/*/__init__.py."""
    src = REPO_ROOT / "src"
    if not src.exists():
        return None, None
    for pkg in sorted(src.iterdir()):
        if not pkg.is_dir():
            continue
        init = pkg / "__init__.py"
        if init.is_file():
            for line in init.read_text(encoding="utf-8").splitlines():
                m = re.match(r'^__version__\s*=\s*"([^"]+)"', line)
                if m:
                    return m.group(1), str(init.relative_to(REPO_ROOT))
    return None, None


def read_version_file() -> str | None:
    p = REPO_ROOT / "VERSION"
    if not p.exists():
        return None
    return p.read_text(encoding="utf-8").strip()


def check_against_tag(tag: str) -> int:
    """Enforce all metadata matches the given tag version."""
    expected = strip_v(tag)
    errors: list[str] = []
    checked = 0

    pv = read_pyproject_version()
    if pv is not None:
        checked += 1
        if pv != expected:
            errors.append(f"pyproject.toml version={pv!r}, expected {expected!r}")
        else:
            print(f"  OK  pyproject.toml version={pv}")

    iv, ipath = read_init_version()
    if iv is not None:
        checked += 1
        if iv != expected:
            errors.append(f"{ipath} __version__={iv!r}, expected {expected!r}")
        else:
            print(f"  OK  {ipath} __version__={iv}")

    vf = read_version_file()
    if vf is not None:
        checked += 1
        expected_vf = expected
        if vf != expected_vf:
            errors.append(f"VERSION file={vf!r}, expected {expected_vf!r}")
        else:
            print(f"  OK  VERSION={vf}")

    if checked == 0:
        print("WARN: No version metadata found to check")
        return 0

    if errors:
        print(f"\nFAIL: Version mismatch against tag {tag}:")
        for e in errors:
            print(f"  - {e}")
        return 1

    print(f"\nPASS: All {checked} version source(s) match tag {tag}")
    return 0


def check_internal_consistency() -> int:
    """Enforce all metadata sources agree with each other (no tag reference)."""
    versions: dict[str, str] = {}

    pv = read_pyproject_version()
    if pv is not None:
        versions["pyproject.toml"] = pv
        print(f"  OK  pyproject.toml version={pv}")

    iv, ipath = read_init_version()
    if iv is not None:
        versions[ipath or "__init__.py"] = iv
        print(f"  OK  {ipath} __version__={iv}")

    vf = read_version_file()
    if vf is not None:
        # Strip leading v for comparison
        versions["VERSION"] = strip_v(vf)
        print(f"  OK  VERSION={vf}")

    if len(versions) == 0:
        print("WARN: No version metadata found to check")
        return 0

    unique = set(versions.values())
    if len(unique) > 1:
        print("\nFAIL: Internal version mismatch:")
        for src, ver in versions.items():
            print(f"  - {src}: {ver}")
        return 1

    print(f"\nPASS: All {len(versions)} version source(s) internally consistent")
    return 0


def main() -> int:
    tag, exact = get_head_tag()

    if tag is not None and exact:
        # HEAD has an exact tag — enforce metadata matches the tag.
        print(f"  TAG  HEAD is tagged {tag}")
        return check_against_tag(tag)

    # HEAD is untagged (dev commit). Enforce internal consistency only.
    print("INFO: HEAD is untagged (dev commit). Tag checks skipped.")
    return check_internal_consistency()


if __name__ == "__main__":
    sys.exit(main())
