#!/usr/bin/env bash
# One-command production deploy.
#
#   ./deploy.sh
#
# Pulls the latest code, rebuilds the images (PHP deps + Vite assets baked in),
# and (re)starts the stack. Migrations and caches run automatically inside the
# app container on startup. Requires a configured .env (see .env.example).
set -euo pipefail

cd "$(dirname "$0")"

COMPOSE="docker compose -f docker-compose.prod.yml"

if [ ! -f .env ]; then
    echo "ERROR: .env is missing. Copy .env.example to .env and configure it" >&2
    echo "       (APP_KEY, APP_ENV=production, APP_DEBUG=false, DOMAIN, ACME_EMAIL, DB_* )." >&2
    exit 1
fi

echo "==> Pulling latest code"
git pull --ff-only

echo "==> Building images"
$COMPOSE build

echo "==> Starting stack"
$COMPOSE up -d --remove-orphans

echo "==> Pruning dangling images"
docker image prune -f >/dev/null || true

echo "==> Done. Status:"
$COMPOSE ps
