#!/bin/bash -eux

REPO_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd ../.. && pwd )"
fname=openapi-$(date +%s).json
curl -o "$REPO_ROOT/$fname" http://localhost:8000/openapi.json

docker run --rm \
    -v "$REPO_ROOT":/local \
    openapitools/openapi-generator-cli \
    generate \
    -i /local/"$fname" \
    -g python \
    -o /local/client/src \
    --additional-properties=generateSourceCodeOnly=true,packageName=openapi_client

# Fix and reformat generated code according to our Ruff rules
ruff check --fix --unsafe-fixes "$REPO_ROOT"/client/src/openapi_client/
ruff format "$REPO_ROOT"/client/src/openapi_client/

# Remove autogenerated artifacts
rm -f "$REPO_ROOT"/client/src/openapi_client_README.md
rm -rf "$REPO_ROOT"/client/src/.openapi-generator/
rm "$REPO_ROOT/$fname"
