# URL Manager — justfile
# Install just: brew install just
# Run `just` to see all available recipes.

set shell := ["bash", "-c"]

# Show available recipes
default:
    @just --list

# ── Setup ─────────────────────────────────────────────────────────────────────

# Install Python dependencies and set up the project for first use
install:
    uv sync
    source .venv/bin/activate && uv run manage.py migrate
    mkdir -p media/screenshots data

# Install Playwright browsers (needed for screenshot capture)
install-browsers:
    source .venv/bin/activate && playwright install chromium

# ── Local Development ─────────────────────────────────────────────────────────

# Run Django server + Tailwind CSS + stern nginx ingestion together
dev:
    #!/usr/bin/env bash
    set -eu
    source .venv/bin/activate
    PIDS=()
    cleanup() {
        echo "Stopping background processes…"
        for pid in "${PIDS[@]}"; do
            kill "$pid" 2>/dev/null || true
        done
    }
    trap cleanup EXIT INT TERM

    npm run dev &
    PIDS+=($!)

    uv run manage.py nginxmon_stern &
    PIDS+=($!)

    uv run manage.py runserver 0.0.0.0:8000

# Stream nginx logs via stern only (without starting Django)
stern:
    #!/usr/bin/env bash
    source .venv/bin/activate
    uv run manage.py nginxmon_stern

# Run Tailwind CSS in watch mode (standalone)
tailwind:
    npm run dev

# ── Database ──────────────────────────────────────────────────────────────────

# Apply all pending migrations
migrate:
    source .venv/bin/activate && uv run manage.py migrate

# Create new migrations from model changes
makemigrations:
    source .venv/bin/activate && uv run manage.py makemigrations

# Open Django shell
shell:
    source .venv/bin/activate && uv run manage.py shell

# Open raw database shell
dbshell:
    source .venv/bin/activate && uv run manage.py dbshell

# Create a Django superuser
superuser:
    source .venv/bin/activate && uv run manage.py createsuperuser

# ── Static Assets ─────────────────────────────────────────────────────────────

# Copy vendor JS/CSS from node_modules to static/vendor/
vendor:
    node scripts/copy-vendor.js

# Build Tailwind CSS (minified, for production)
build-css:
    npm run build:css

# Build the React search component with Vite
build-search:
    npm run build:search

# Build all frontend assets: vendor copy + Tailwind + Vite (full production build)
build:
    npm run build

# Collect all static files into staticfiles/
collectstatic:
    source .venv/bin/activate && uv run manage.py collectstatic --no-input

# ── i18n ──────────────────────────────────────────────────────────────────────

# Compile translation files
compilemessages:
    source .venv/bin/activate && uv run manage.py compilemessages

# Extract translatable strings for zh_Hans
makemessages:
    source .venv/bin/activate && uv run manage.py makemessages -l zh_Hans

# ── Testing ───────────────────────────────────────────────────────────────────

# Run tests
test:
    source .venv/bin/activate && pytest

# Run tests with coverage
test-cov:
    source .venv/bin/activate && pytest --cov=links --cov-report=term-missing

# ── Docker ────────────────────────────────────────────────────────────────────

# Build Docker images
docker-build:
    docker-compose build

# Start all Docker services (detached)
docker-start:
    mkdir -p media/screenshots
    chmod -R 777 media
    docker-compose up -d
    @echo "App running at http://localhost:8000"

# Stop all Docker services
docker-stop:
    docker-compose down

# Restart Docker services
docker-restart:
    docker-compose restart

# Tail logs for all services (or a specific one: just docker-logs web)
docker-logs service="":
    #!/usr/bin/env bash
    if [ -n "{{ service }}" ]; then
        docker-compose logs -f {{ service }}
    else
        docker-compose logs -f
    fi

# Open a shell inside the web container
docker-shell:
    docker-compose exec web bash

# Run migrations inside Docker
docker-migrate:
    docker-compose exec web uv run manage.py makemigrations
    docker-compose exec web uv run manage.py migrate

# Collect static files inside Docker
docker-static:
    docker-compose exec web uv run manage.py collectstatic --no-input

# Rebuild and restart a specific service (e.g.: just docker-rebuild web)
docker-rebuild service="web":
    docker-compose up -d --build {{ service }}
