#!/usr/bin/env python
import os
import subprocess
import sys
from pathlib import Path

if __name__ == "__main__":
    py_version = sys.version_info[:3]

    os.chdir(Path(__file__).parent)
    os.environ["CUSTOM_COMPILE_COMMAND"] = "requirements/compile.py"
    os.environ["PIP_REQUIRE_VIRTUALENV"] = "0"
    common_args = [
        "-m",
        "piptools",
        "compile",
        "--generate-hashes",
        "--allow-unsafe",
        "--no-emit-index-url"
    ] + sys.argv[1:]

    if (3, 10, 0) <= py_version < (3, 11, 0):
        subprocess.run(
            [
                "python",
                *common_args,
                "-P",
                "Django>=4.2.26,<5",
                "-P",
                "urllib3>=2.6.0",
                "-P",
                "sqlparse>=0.5.4",
                "-o",
                "py310-django42.txt",
            ],
            check=True,
            capture_output=True,
        )
        subprocess.run(
            [
                "python",
                *common_args,
                "-P",
                "Django>=5.1.14,<5.2",
                "-P",
                "urllib3>=2.6.0",
                "-o",
                "py310-django51.txt",
            ],
            check=True,
            capture_output=True,
        )
        subprocess.run(
            [
                "python",
                *common_args,
                "-P",
                "Django>=5.2.8,<6.0",
                "-P",
                "urllib3>=2.6.0",
                "-o",
                "py310-django52.txt",
            ],
            check=True,
            capture_output=True,
        )
    if (3, 11, 0) <= py_version < (3, 12, 0):
        subprocess.run(
            [
                "python",
                *common_args,
                "-P",
                "Django>=4.2.26,<5",
                "-P",
                "urllib3>=2.6.0",
                "-P",
                "sqlparse>=0.5.4",
                "-o",
                "py311-django42.txt",
            ],
            check=True,
            capture_output=True,
        )
        # Django 5 required Python 3.10+
        subprocess.run(
            [
                "python",
                *common_args,
                "-P",
                "Django>=5.1.14,<5.2",
                "-P",
                "urllib3>=2.6.0",
                "-P",
                "sqlparse>=0.5.4",
                "-o",
                "py311-django51.txt",
            ],
            check=True,
            capture_output=True,
        )
        subprocess.run(
            [
                "python",
                *common_args,
                "-P",
                "Django>=5.2.8,<6.0",
                "-P",
                "urllib3>=2.6.0",
                "-P",
                "sqlparse>=0.5.4",
                "-o",
                "py311-django52.txt",
            ],
            check=True,
            capture_output=True,
        )
    if (3, 12, 0) <= py_version < (3, 13, 0):
        subprocess.run(
            [
                "python",
                *common_args,
                "-P",
                "Django>=4.2.26,<5",
                "-P",
                "urllib3>=2.6.0",
                "-P",
                "sqlparse>=0.5.4",
                "-o",
                "py312-django42.txt",
            ],
            check=True,
            capture_output=True,
        )
        subprocess.run(
            [
                "python",
                *common_args,
                "-P",
                "Django>=5.1.14,<5.2",
                "-P",
                "urllib3>=2.6.0",
                "-o",
                "py312-django51.txt",
            ],
            check=True,
            capture_output=True,
        )
        subprocess.run(
            [
                "python",
                *common_args,
                "-P",
                "Django>=5.2.8,<6.0",
                "-P",
                "urllib3>=2.6.0",
                "-o",
                "py312-django52.txt",
            ],
            check=True,
            capture_output=True,
        )
        subprocess.run(
            [
                "python",
                *common_args,
                "-P",
                "Django>=6.0,<6.1",
                "-P",
                "urllib3>=2.6.0",
                "-o",
                "py312-django60.txt",
            ],
            check=True,
            capture_output=True,
        )
    if (3, 13, 0) <= py_version < (3, 15, 0):
        # Python 3.13
        subprocess.run(
            [
                "python",
                *common_args,
                "-P",
                "Django>=4.2.26,<5",
                "-P",
                "urllib3>=2.6.0",
                "-P",
                "sqlparse>=0.5.4",
                "-o",
                "py313-django42.txt",
            ],
            check=True,
            capture_output=True,
        )
        subprocess.run(
            [
                "python",
                *common_args,
                "-P",
                "Django>=5.1.14,<5.2",
                "-P",
                "urllib3>=2.6.0",
                "-o",
                "py313-django51.txt",
            ],
            check=True,
            capture_output=True,
        )
        subprocess.run(
            [
                "python",
                *common_args,
                "-P",
                "Django>=5.2.8,<6.0",
                "-P",
                "urllib3>=2.6.0",
                "-o",
                "py313-django52.txt",
            ],
            check=True,
            capture_output=True,
        )
        subprocess.run(
            [
                "python",
                *common_args,
                "-P",
                "Django>=6.0,<6.1",
                "-P",
                "urllib3>=2.6.0",
                "-o",
                "py313-django60.txt",
            ],
            check=True,
            capture_output=True,
        )
        # Python 3.14
        subprocess.run(
            [
                "python",
                *common_args,
                "-P",
                "Django>=4.2.26,<5",
                "-P",
                "urllib3>=2.6.0",
                "-P",
                "sqlparse>=0.5.4",
                "-o",
                "py314-django42.txt",
            ],
            check=True,
            capture_output=True,
        )
        subprocess.run(
            [
                "python",
                *common_args,
                "-P",
                "Django>=5.1.14,<5.2",
                "-P",
                "urllib3>=2.6.0",
                "-o",
                "py314-django51.txt",
            ],
            check=True,
            capture_output=True,
        )
        subprocess.run(
            [
                "python",
                *common_args,
                "-P",
                "Django>=5.2.8,<6.0",
                "-P",
                "urllib3>=2.6.0",
                "-o",
                "py314-django52.txt",
            ],
            check=True,
            capture_output=True,
        )
        subprocess.run(
            [
                "python",
                *common_args,
                "-P",
                "Django>=6,<6.1",
                "-P",
                "urllib3>=2.6.0",
                "-o",
                "py314-django60.txt",
            ],
            check=True,
            capture_output=True,
        )
