default:
    @just --list

[doc("Proxy on the host, for example: just serve 8080")]
serve port="8080":
    DJANGO_SETTINGS_MODULE=demo.settings uv run gateway --port {{port}}

[doc("Dashboard on the host: http://localhost:8000/")]
dashboard port="8000":
    uv run python demo/manage.py collectstatic --noinput -v 0
    uv run uvicorn demo.asgi:application --port {{port}}

migrate:
    uv run python demo/manage.py migrate

test:
    uv run pytest -q

[doc("What CI runs: ruff, Django checks, missing migrations, tests with coverage")]
check:
    gitleaks git --redact --no-banner .
    uv run djlint src/django_ai_gateway/templates demo/templates --check
    uv run djlint src/django_ai_gateway/templates demo/templates --lint
    uv run ruff check .
    uv run ruff format --check .
    uv run ty check
    uv run python demo/manage.py check --settings=tests.settings
    uv run python demo/manage.py makemigrations --check --dry-run --settings=tests.settings
    uv run pytest -q

[doc("Rebuild the dashboard stylesheet after changing classes in a template (needs pnpm)")]
css:
    cd frontend && pnpm install --frozen-lockfile && pnpm build

[doc("Fix and format with ruff, then test")]
lint:
    gitleaks git --redact --no-banner .
    uv run djlint src/django_ai_gateway/templates demo/templates --reformat --quiet
    uv run djlint src/django_ai_gateway/templates demo/templates --lint
    uv run ruff check --fix
    uv run ruff format .
    uv run ty check
    uv run pytest -q

[doc("Proxy and dashboard in Docker behind portside: inference.gateway.test, admin.gateway.test")]
up:
    docker compose up -d --build

down:
    docker compose down

logs:
    docker compose logs -f proxy dashboard

[doc("Local TLS certificate for both containers (mkcert)")]
certs:
    mkcert -cert-file certs/cert.pem -key-file certs/key.pem gateway.test '*.gateway.test' gateway-nginx gateway-admin-nginx localhost 127.0.0.1

[doc("Rebuild the plans menu after adding a page to plans/")]
plans:
    uv run python plans/menu.py

[doc("Install the git hooks (prek reads .pre-commit-config.yaml)")]
hooks:
    uv run prek install

[doc("Tag a release, for example: just tag v0.0.2 (CI builds and publishes from the tag)")]
tag version:
    git tag -a {{version}} -m "Release {{version}}"

[doc("Build the wheel and sdist into dist/; the version comes from the latest tag")]
build:
    uv build

[doc("Check the built files the way PyPI renders them")]
check-dist:
    uvx twine check dist/*

[doc("Check the docs and build the site into public/")]
docs-build:
    uv run --group docs dokk check --root docs
    uv run --group docs dokk build --root docs

[doc("Serve the docs locally with live reload")]
docs-serve:
    uv run --group docs dokk serve --root docs

[doc("Fill the database with demo providers, projects and 30 days of calls")]
seed *args:
    uv run python demo/manage.py seed_demo {{args}}

[doc("Dashboard screenshots for the docs, from demo data in a throwaway database (needs Chrome)")]
screenshots:
    #!/usr/bin/env bash
    set -euo pipefail
    tmp="$(mktemp -d)"
    trap 'rm -rf "$tmp"' EXIT
    export DATABASE_URL="sqlite:///$tmp/docs.sqlite3"
    uv run python demo/manage.py migrate -v0
    uv run python demo/manage.py seed_demo > /dev/null
    uv run python demo/manage.py docs_screenshots
