import json
import sys
from pathlib import Path

import yaml

PROJECT_ROOT = Path(__file__).resolve().parents[1]
if str(PROJECT_ROOT) not in sys.path:
    sys.path.insert(0, str(PROJECT_ROOT))

from app.main import create_app


def main() -> None:
    app = create_app()
    output_dir = PROJECT_ROOT / "openapi"
    output_dir.mkdir(parents=True, exist_ok=True)

    schema = app.openapi()

    json_path = output_dir / "openapi.json"
    yaml_path = output_dir / "openapi.yaml"

    json_path.write_text(json.dumps(schema, ensure_ascii=False, indent=2), encoding="utf-8")
    yaml_path.write_text(yaml.safe_dump(schema, allow_unicode=True, sort_keys=False), encoding="utf-8")

    print(f"Wrote {json_path}")
    print(f"Wrote {yaml_path}")


if __name__ == "__main__":
    main()
