set dotenv-load

# progi task runner
#
# Quickstart:
#   just install     # python deps + vendored JS + tailwind CLI
#   just build       # compile CSS
#   just dev         # run web app with reload
#
# Versions are pinned; bump them here.

tailwind_version := "v4.1.18"
alpine_version := "3.15.4"
alpine_ajax_version := "0.12.4"
mermaid_version := "11.15.0"
marked_version := "18.0.5"

# ---------------------------------------------------------------------------
# Dev
# ---------------------------------------------------------------------------

# Run the web app with autoreload + CSS watch (web only; MCP not involved here).
dev:
    #!/usr/bin/env bash
    set -euo pipefail
    cd frontend && ./tailwindcss -i input.css -o ../progi/web/static/style.css --watch &
    trap "kill 0" EXIT
    uv run python -m uvicorn progi.web.app:app --reload

# Run the web app with autoreload only (no CSS watch).
uvicorn:
    uv run python -m uvicorn progi.web.app:app --reload

# Watch and rebuild CSS while developing (run alongside `just dev`).
watch-css:
    cd frontend && ./tailwindcss -i input.css -o ../progi/web/static/style.css --watch

# Run the MCP server (stdio) only
mcp:
    uv run progi --no-web

# ---------------------------------------------------------------------------
# Install
# ---------------------------------------------------------------------------

install:
    just install-python
    just vendorize
    just install-tailwind-cli

install-python:
    uv sync --extra dev

# Vendor frontend JS locally (no runtime CDN). Alpine plugins must be present
# before Alpine core loads; base.html orders the <script> tags accordingly.
vendorize:
    just _vendorize alpine {{alpine_version}} "https://unpkg.com/alpinejs@{{alpine_version}}/dist/cdn.min.js"
    just _vendorize alpine-ajax {{alpine_ajax_version}} "https://unpkg.com/@imacrayon/alpine-ajax@{{alpine_ajax_version}}/dist/cdn.min.js"
    just _vendorize mermaid {{mermaid_version}} "https://cdn.jsdelivr.net/npm/mermaid@{{mermaid_version}}/dist/mermaid.min.js"
    just _vendorize marked {{marked_version}} "https://cdn.jsdelivr.net/npm/marked@{{marked_version}}/lib/marked.umd.js"

_vendorize name version url:
    #!/usr/bin/env bash
    set -euo pipefail
    location="progi/web/static/vendor/{{name}}.min.js"
    curl -sL -o "$location" "{{url}}"
    echo "✓ Vendorized {{name}}.min.js"

# Download the Tailwind standalone CLI.
# Works on Linux, macOS, Windows (git-bash). No Node required.
install-tailwind-cli:
    #!/usr/bin/env bash
    set -euo pipefail

    os=$(uname -s | tr '[:upper:]' '[:lower:]')
    arch=$(uname -m)

    case "$os" in
        linux)        platform="linux";   ext="" ;;
        darwin)       platform="macos";   ext="" ;;
        mingw*|msys*|cygwin*) platform="windows"; ext=".exe" ;;
        *) echo "Unsupported OS: $os"; exit 1 ;;
    esac

    case "$arch" in
        x86_64|amd64) arch_name="x64" ;;
        arm64|aarch64) arch_name="arm64" ;;
        *) echo "Unsupported architecture: $arch"; exit 1 ;;
    esac

    filename="tailwindcss-${platform}-${arch_name}${ext}"
    url="https://github.com/tailwindlabs/tailwindcss/releases/download/{{tailwind_version}}/${filename}"
    output="frontend/tailwindcss${ext}"

    echo "Downloading Tailwind CSS CLI {{tailwind_version}} for ${platform}-${arch_name}..."
    curl -sL -o "$output" "$url"
    chmod +x "$output"
    echo "✓ Installed Tailwind CSS CLI to ${output}"

# ---------------------------------------------------------------------------
# Build
# ---------------------------------------------------------------------------

build:
    just build-tailwind

build-tailwind:
    cd frontend && ./tailwindcss -i input.css -o "../progi/web/static/style.css" --minify

# ---------------------------------------------------------------------------
# DB / migrations
# ---------------------------------------------------------------------------

# Create a new autogenerated migration: just migrate "add users table"
migrate message:
    uv run alembic revision --autogenerate -m "{{message}}"

# Apply migrations.
upgrade:
    uv run alembic upgrade head

# Load the "Blog Post" workflow + a sample task (idempotent).
seed:
    uv run python -m progi.seed

# ---------------------------------------------------------------------------
# Quality
# ---------------------------------------------------------------------------

lint:
    uv run ruff check progi

fmt:
    uv run ruff format progi

test:
    uv run python -m pytest
