#!/bin/bash
set -e

COMPOSE_FILE="docker-compose.prod.yml"

echo "========================================"
echo "  PX360 Production Deploy"
echo "  Pulling latest image"
echo "  $(date '+%Y-%m-%d %H:%M:%S')"
echo "========================================"

echo ""
echo "[1/5] Pulling latest images..."
docker compose -f "$COMPOSE_FILE" pull

echo ""
echo "[2/5] Running migrations..."
docker compose -f "$COMPOSE_FILE" run --rm --no-deps web python manage.py migrate --noinput

echo ""
echo "[3/5] Restarting all services..."
docker compose -f "$COMPOSE_FILE" up -d --remove-orphans --force-recreate

echo ""
echo "[4/5] Collecting static files..."
docker compose -f "$COMPOSE_FILE" exec web python manage.py collectstatic --noinput

echo ""
echo "[5/5] Cleaning up old images..."
docker image prune -f

echo ""
echo "========================================"
echo "  Deploy complete!"
echo "  $(date '+%Y-%m-%d %H:%M:%S')"
echo "========================================"
echo ""
docker compose -f "$COMPOSE_FILE" ps
echo ""
echo "Logs: docker compose -f $COMPOSE_FILE logs -f"
