#!/usr/bin/env python3
"""Check that package.json version matches __version__.py."""

import json
import re
import sys
from pathlib import Path


def main():
    root = Path(__file__).parent.parent

    # Read __version__.py
    version_file = root / "src" / "local_deep_research" / "__version__.py"
    if not version_file.exists():
        print(f"ERROR: Version file not found: {version_file}")
        return 1

    try:
        version_content = version_file.read_text()
    except OSError as e:
        print(f"ERROR: Could not read {version_file}: {e}")
        return 1

    match = re.search(r'__version__\s*=\s*["\']([^"\']+)["\']', version_content)
    if not match:
        print("ERROR: Could not parse __version__.py")
        return 1
    py_version = match.group(1)

    # Read package.json
    package_file = root / "package.json"
    if not package_file.exists():
        print(f"ERROR: package.json not found: {package_file}")
        return 1

    try:
        package_content = package_file.read_text()
        package_data = json.loads(package_content)
    except OSError as e:
        print(f"ERROR: Could not read {package_file}: {e}")
        return 1
    except json.JSONDecodeError as e:
        print(f"ERROR: Invalid JSON in {package_file}: {e}")
        return 1

    js_version = package_data.get("version", "")
    if not js_version:
        print("ERROR: No 'version' field found in package.json")
        return 1

    if py_version != js_version:
        print("ERROR: Version mismatch!")
        print(f"  __version__.py: {py_version}")
        print(f"  package.json:   {js_version}")
        print()
        print("Fix by updating one of the files to match.")
        return 1

    return 0


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