#!/bin/bash

# 🐳 Plesk Docker Deployment Script
# Updated for organized Docker structure

set -e

# Configuration
REPO_PATH="/var/www/vhosts/yourdomain.com/git/customer-portal"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

log() { echo -e "${GREEN}[PLESK] $1${NC}"; }
warn() { echo -e "${YELLOW}[PLESK] WARNING: $1${NC}"; }
error() { echo -e "${RED}[PLESK] ERROR: $1${NC}"; exit 1; }

# Navigate to repository
cd "$REPO_PATH"

log "🚀 Starting Plesk Docker deployment..."

# Check if Docker is available
if ! command -v docker &> /dev/null; then
    error "Docker is not installed. Please install Docker first."
fi

if ! command -v docker-compose &> /dev/null; then
    error "Docker Compose is not installed. Please install Docker Compose."
fi

# Check if production environment exists
ENV_FILE=".env"
if [ ! -f "$ENV_FILE" ]; then
    log "Creating environment file from template..."
    cp .env.production.example .env
    warn "Please edit .env with your actual production values!"
    error "Production environment not configured. Please set up .env"
fi

# Use the organized production management script
log "Running production deployment script..."
./scripts/prod/manage.sh deploy

log "🎉 Plesk Docker deployment completed!"
log "📝 Don't forget to:"
echo "1. Configure Plesk reverse proxy to point to port 3000"
echo "2. Set up SSL certificates in Plesk"
echo "3. Test your application at your domain"
