# paste-it development tasks

export COMPOSE_FILE := "docker-compose.yml:docker-compose.dev.yml"

# Start full production stack (depends on db: brings up postgres + applies migrations first)
up: db
    docker compose up --build -d

# Tear down full stack (including volumes)
down:
    docker compose down -v

# Start postgres for local dev and apply any pending migrations (idempotent)
db:
    docker compose up -d --wait postgres
    cd backend && bun run drizzle-kit migrate

# Stop postgres
db-stop:
    docker compose stop postgres

# Run backend + frontend together; Ctrl+C stops both (and postgres)
dev: db
    #!/usr/bin/env bash
    cleanup() {
        trap - INT TERM EXIT
        kill $(jobs -p) 2>/dev/null
        wait 2>/dev/null
        just db-stop
    }
    trap cleanup INT TERM EXIT
    (cd backend && bun run dev) &
    (cd frontend && bun run dev) &
    wait

# Format the whole repo (Prettier + prettier-plugin-svelte)
fmt:
    bunx prettier --write .

# Check formatting without writing
fmt-check:
    bunx prettier --check .

# Lint the whole repo (ESLint + plugin-svelte + typescript-eslint)
lint:
    bunx eslint .

# Live-preview the presentation in browser (Slidev dev server with hot reload)
slides:
    ./node_modules/.bin/slidev slides.md --open

# Demo: show Caddy round-robin across the two backends (requires `just up`)
lb-rr:
    ./scripts/lb-roundrobin.sh

# Demo: stop backend-1 and watch backend-2 keep serving (requires `just up`)
lb-failover:
    ./scripts/lb-failover.sh

# Load test a session-protected endpoint with oha (requires `just up`)
load-test:
    ./scripts/load-test.sh

# Apply pending Drizzle migrations (requires postgres reachable on localhost:5432)
migrate:
    cd backend && bun run drizzle-kit migrate

# Generate a new Drizzle migration from schema changes
migrate-gen name="":
    cd backend && bun run drizzle-kit generate {{ if name == "" { "" } else { "--name " + name } }}

# One-time local dev setup: create .env files and fill in a real JWT_SECRET
setup:
    #!/usr/bin/env bash
    set -e
    for f in .env backend/.env; do
        [ -f "$f" ] || cp "$f.example" "$f"
    done
    if grep -qE '^JWT_SECRET=(change|replace)-' .env backend/.env; then
        sed -i "s|^JWT_SECRET=.*|JWT_SECRET=$(openssl rand -base64 32)|" .env backend/.env
        echo "Generated JWT_SECRET in both .env files"
    fi
