#!/usr/bin/env python3
# File generated by pre-commit: https://pre-commit.com
# ID: 138fd403232d2ddd5efb44317e38bf03
# Modified to be a Python script for cross-platform compatibility

import os
import subprocess
import sys


def main():
    """Main function to execute pre-commit hook."""
    print("✓ Pre-commit hook is running...")
    print("✓ Pre-commit钩子正在运行...")

    # Get the directory where this script is located
    here = os.path.dirname(os.path.abspath(__file__))

    # Generate arguments
    args = [
        "hook-impl",
        "--config=.wl/.pre-commit-config.yaml",
        "--hook-type=pre-commit",
        "--hook-dir",
        here,
        "--",
    ] + sys.argv[1:]

    # Try to find Python executable
    python_paths = [
        # Try uv Python first
        "uv",
        "python",
        "--print-path",
        # Try virtual environment paths
        os.path.join(".venv", "bin", "python"),
        os.path.join(".venv", "bin", "python3"),
        os.path.join("..", ".venv", "bin", "python"),
        os.path.join("..", ".venv", "bin", "python3"),
        os.path.join("../../", ".venv", "bin", "python"),
        os.path.join("../../", ".venv", "bin", "python3"),
        # Windows virtual environment paths
        os.path.join(".venv", "Scripts", "python.exe"),
        os.path.join(".venv", "Scripts", "python3.exe"),
        os.path.join("..", ".venv", "Scripts", "python.exe"),
        os.path.join("..", ".venv", "Scripts", "python3.exe"),
        # System Python
        "python",
        "python3",
    ]

    python_exe = None

    # Try uv first
    try:
        uv_result = subprocess.run(
            ["uv", "python", "--print-path"], capture_output=True, text=True, check=True
        )
        python_exe = uv_result.stdout.strip()
    except (subprocess.CalledProcessError, FileNotFoundError):
        # Try other paths
        for path in python_paths[1:]:
            if os.path.isabs(path):
                abs_path = path
            else:
                abs_path = os.path.join(os.getcwd(), path)

            if os.path.exists(abs_path):
                python_exe = abs_path
                break

    if not python_exe:
        print(
            "`pre-commit` not found.  Did you forget to activate your virtualenv?",
            file=sys.stderr,
        )
        return 1

    # Execute pre_commit
    if python_exe == "python" or python_exe == "python3":
        # Direct Python call
        result = subprocess.run([python_exe, "-m", "pre_commit"] + args, check=False)
    else:
        # Use the specific Python path
        result = subprocess.run([python_exe, "-m", "pre_commit"] + args, check=False)

    return result.returncode


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