from __future__ import annotations

import argparse
import subprocess
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
SPEC = ROOT / "swagger.json"
OUTPUT_ROOT = ROOT / "generated"


def build_command(mode: str) -> list[str]:
    library = "httpx" if mode == "sync" else "asyncio"
    package_name = f"easybill_generated_{mode}"
    project_name = f"easybill-generated-{mode}"
    output_path = OUTPUT_ROOT / mode

    return [
        "docker",
        "run",
        "--rm",
        "-v",
        f"{ROOT}:/local",
        "openapitools/openapi-generator-cli",
        "generate",
        "-i",
        "/local/swagger.json",
        "-g",
        "python",
        "-o",
        f"/local/generated/{mode}",
        "--skip-validate-spec",
        "--additional-properties",
        f"library={library},packageName={package_name},projectName={project_name},hideGenerationTimestamp=true",
    ]


def main() -> int:
    parser = argparse.ArgumentParser(description="Generate the raw easybill client from swagger.json")
    parser.add_argument("--mode", choices=["sync", "async", "both"], default="both")
    args = parser.parse_args()

    if not SPEC.exists():
        raise SystemExit("swagger.json not found")

    OUTPUT_ROOT.mkdir(exist_ok=True)
    modes = ["sync", "async"] if args.mode == "both" else [args.mode]

    for mode in modes:
        command = build_command(mode)
        print("Running:", " ".join(command))
        subprocess.run(command, check=True)

    return 0


if __name__ == "__main__":
    raise SystemExit(main())
