#!/usr/bin/env python3
"""Check that golden master settings are updated when default settings change."""

import subprocess
import sys


def _find_venv_python():
    """Find the project's venv Python interpreter."""
    import pathlib

    for candidate in [
        pathlib.Path(".venv/bin/python"),
        pathlib.Path("venv/bin/python"),
    ]:
        if candidate.exists():
            return str(candidate)
    return sys.executable


def _golden_master_is_stale():
    """Regenerate golden master in-place and check if git sees a diff."""
    golden_master = "tests/settings/golden_master_settings.json"
    python = _find_venv_python()
    try:
        result = subprocess.run(
            [python, "scripts/dev/regenerate_golden_master.py"],
            capture_output=True,
            text=True,
            timeout=30,
        )
        if result.returncode != 0:
            return True
        # Check if regeneration changed the file
        diff = subprocess.run(
            ["git", "diff", "--quiet", golden_master],
            capture_output=True,
        )
        # Restore original
        subprocess.run(
            ["git", "checkout", "--", golden_master],
            capture_output=True,
        )
        return diff.returncode != 0
    except Exception:
        return True


def main():
    result = subprocess.run(
        ["git", "diff", "--cached", "--name-only"],
        capture_output=True,
        text=True,
    )
    if result.returncode != 0:
        print("Warning: Could not check staged files (git error)")
        return 0
    staged_files = result.stdout.strip().splitlines()

    defaults_changed = any(
        f.startswith("src/local_deep_research/defaults/")
        and f.endswith(".json")
        for f in staged_files
    )

    if not defaults_changed:
        return 0

    golden_master = "tests/settings/golden_master_settings.json"
    if golden_master in staged_files:
        return 0

    # Only fail if the golden master is actually out of date
    if not _golden_master_is_stale():
        return 0

    print("ERROR: Default settings changed but golden master not updated!")
    print()
    print("  Staged files that affect settings:")
    for f in staged_files:
        if f.startswith("src/local_deep_research/defaults/") and f.endswith(
            ".json"
        ):
            print(f"    - {f}")
    print()
    print("  To regenerate the golden master:")
    print("    1. Run: python scripts/dev/regenerate_golden_master.py")
    print(f"    2. Stage the updated {golden_master}")
    return 1


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