# ── LibNovel v3 — justfile ────────────────────────────────────────────────────
# All commands that touch docker-compose are wrapped with `doppler run` so that
# secrets are injected into the environment at runtime — no .env files needed.
#
# Prerequisites:
#   brew install doppler just
#   doppler setup   (run once; selects project=libnovel config=prd)
#
# Usage:
#   just up          # start all services (detached)
#   just down        # stop all services
#   just logs        # tail all service logs
#   just ps          # show running containers
#   just build       # rebuild backend + ui images
#   just restart     # full stop + start cycle
#   just secrets     # print all injected secrets (debug)

set dotenv-load := false  # Doppler handles all env; never load a .env file

# ── Helpers ───────────────────────────────────────────────────────────────────

# Inject secrets from Doppler, then run the given command
doppler := "doppler run --"

# ── Core compose commands ─────────────────────────────────────────────────────

# Start all services in the background
up:
    {{doppler}} docker compose up -d

# Start and stream logs (foreground)
up-fg:
    {{doppler}} docker compose up

# Stop all running services
down:
    {{doppler}} docker compose down

# Stop and remove volumes (full reset — destructive!)
down-volumes:
    {{doppler}} docker compose down -v

# Show service status
ps:
    {{doppler}} docker compose ps

# ── Build & publish ───────────────────────────────────────────────────────────

# Build (or rebuild) all custom images locally
build:
    {{doppler}} docker compose build

# Build a specific service, e.g.: just build-svc backend
build-svc svc:
    {{doppler}} docker compose build {{svc}}

# Push all custom images to Docker Hub (requires docker login)
push:
    {{doppler}} docker compose push backend runner ui caddy

# Build then push all custom images
build-push: build push

# Pull all images from Docker Hub (uses GIT_TAG from Doppler)
pull-images:
    {{doppler}} docker compose pull backend runner ui caddy

# Pull all third-party base images (minio, pocketbase, etc.)
pull-infra:
    {{doppler}} docker compose pull minio pocketbase meilisearch valkey postgres crowdsec watchtower

# ── Logs ─────────────────────────────────────────────────────────────────────

# Tail all service logs (last 50 lines + follow)
logs:
    {{doppler}} docker compose logs -f --tail=50

# Tail a specific service, e.g.: just log backend
log svc:
    {{doppler}} docker compose logs -f --tail=50 {{svc}}

# ── Lifecycle ─────────────────────────────────────────────────────────────────

# Full restart: stop then start
restart: down up

# Restart a single service, e.g.: just restart-svc backend
restart-svc svc:
    {{doppler}} docker compose restart {{svc}}

# Pull → build → recreate (rolling update without clearing volumes)
update:
    {{doppler}} docker compose pull
    {{doppler}} docker compose build
    {{doppler}} docker compose up -d

# ── Initialisation ────────────────────────────────────────────────────────────

# Run one-shot init containers (minio-init, pb-init, postgres-init)
init:
    {{doppler}} docker compose run --rm minio-init
    {{doppler}} docker compose run --rm pb-init
    {{doppler}} docker compose run --rm postgres-init

# ── Shell access ──────────────────────────────────────────────────────────────

# Open a shell in a running service, e.g.: just shell backend
shell svc:
    {{doppler}} docker compose exec {{svc}} sh

# ── Secrets ───────────────────────────────────────────────────────────────────

# Print all secrets Doppler will inject (never redirected to a file)
secrets:
    doppler secrets --project libnovel --config prd

# Print secrets as a .env-formatted list (useful for debugging)
secrets-env:
    doppler secrets download --project libnovel --config prd --format env --no-file

# Open Doppler dashboard in browser
secrets-dashboard:
    doppler open dashboard

# ── Developer setup ───────────────────────────────────────────────────────────

# One-time dev setup: configure git to use committed hooks in .githooks/
setup:
    git config core.hooksPath .githooks
    @echo "Git hooks configured (.githooks/pre-commit active)."

# ── Gitea CI ──────────────────────────────────────────────────────────────────

# Validate workflow files
ci-lint:
    actionlint .gitea/workflows/*.yaml
