#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"
die(){ echo "DEPLOYMENT BLOCKED: $*" >&2; exit 1; }
[[ -s composer.lock ]] || die "composer.lock is missing. Generate, review and commit it first."
[[ -f .env ]] || die ".env is missing."
grep -Eq '^APP_ENV=production$' .env || die "APP_ENV must be production."
grep -Eq '^APP_DEBUG=false$' .env || die "APP_DEBUG must be false."
grep -Eq '^APP_KEY=base64:.{20,}$' .env || die "APP_KEY is missing."
grep -Eq '^DB_PASSWORD=.{16,}$' .env || die "DB_PASSWORD must be at least 16 characters."
grep -Eq '^MYSQL_ROOT_PASSWORD=.{20,}$' .env || die "MYSQL_ROOT_PASSWORD must be at least 20 characters."
command -v docker >/dev/null 2>&1 || die "Docker is unavailable."
docker compose config --quiet
docker compose build --pull
docker compose up -d mysql
docker compose up -d app nginx queue scheduler
docker compose exec -T app php artisan migrate:status
echo "Containers started. This script deliberately does not run migrations automatically."
echo "Review migration status, take a backup, then run: docker compose exec app php artisan migrate --force"
echo "Run smoke test and role-based acceptance before opening staging to testers."
