# ASF TestArena Deployment Script (PowerShell)
# This script deploys the TestArena application using Docker Compose

$ErrorActionPreference = "Stop"

Write-Host "==========================================" -ForegroundColor Cyan
Write-Host "  ASF TestArena Deployment Script" -ForegroundColor Cyan
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host ""

function Write-Success {
    param($Message)
    Write-Host "✓ $Message" -ForegroundColor Green
}

function Write-Error-Custom {
    param($Message)
    Write-Host "✗ $Message" -ForegroundColor Red
}

function Write-Warning-Custom {
    param($Message)
    Write-Host "⚠ $Message" -ForegroundColor Yellow
}

function Write-Info {
    param($Message)
    Write-Host "ℹ $Message" -ForegroundColor Yellow
}

# Check if Docker is installed
Write-Host "Checking prerequisites..."
try {
    $null = docker --version
    Write-Success "Docker is installed"
} catch {
    Write-Error-Custom "Docker is not installed. Please install Docker Desktop first."
    exit 1
}

# Check if Docker Compose is available
try {
    $null = docker-compose --version
    Write-Success "Docker Compose is installed"
} catch {
    try {
        $null = docker compose version
        Write-Success "Docker Compose is installed"
    } catch {
        Write-Error-Custom "Docker Compose is not installed. Please install Docker Compose first."
        exit 1
    }
}

# Check if Docker daemon is running
try {
    $null = docker info 2>&1
    Write-Success "Docker daemon is running"
} catch {
    Write-Error-Custom "Docker daemon is not running. Please start Docker Desktop first."
    exit 1
}

Write-Host ""

# Check if .env file exists
if (-not (Test-Path .env)) {
    Write-Warning-Custom ".env file not found. Creating from .env.example..."
    if (Test-Path .env.example) {
        Copy-Item .env.example .env
        Write-Info "Please edit .env file and update SECRET_KEY and passwords before continuing."
        Read-Host "Press Enter to continue after editing .env, or Ctrl+C to exit"
    } else {
        Write-Error-Custom ".env.example not found. Cannot create .env file."
        exit 1
    }
}

Write-Host ""
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host "  Starting Deployment" -ForegroundColor Cyan
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host ""

# Check if caddy_network exists
Write-Host "Checking for Caddy network..."
try {
    $null = docker network inspect caddy_network 2>&1
    Write-Success "Caddy network (caddy_network) exists"
} catch {
    Write-Warning-Custom "Caddy network (caddy_network) not found"
    $response = Read-Host "Do you want to create it? (y/n)"
    if ($response -eq 'y' -or $response -eq 'Y') {
        docker network create caddy_network
        Write-Success "Created caddy_network"
    } else {
        Write-Error-Custom "Caddy network is required. Exiting."
        exit 1
    }
}

Write-Host ""

# Stop existing containers if running
Write-Host "Stopping existing containers (if any)..."
try {
    docker-compose down 2>$null
} catch {
    # Ignore errors if containers don't exist
}
Write-Success "Stopped existing containers"

Write-Host ""

# Build and start containers
Write-Host "Building and starting containers..."
Write-Host "This may take a few minutes on first run..."
Write-Host ""

try {
    docker-compose up -d --build
    Write-Success "Containers built and started successfully"
} catch {
    Write-Error-Custom "Failed to start containers"
    Write-Host ""
    Write-Host "Check logs with: docker-compose logs"
    exit 1
}

Write-Host ""
Write-Host "Waiting for services to initialize..."
Start-Sleep -Seconds 5

# Check if containers are running
Write-Host ""
Write-Host "Checking container status..."
$webRunning = docker ps | Select-String "testarena_web"
$dbRunning = docker ps | Select-String "testarena_db"

if ($webRunning -and $dbRunning) {
    Write-Success "All containers are running"
} else {
    Write-Error-Custom "Some containers failed to start"
    Write-Host ""
    Write-Host "Container status:"
    docker-compose ps
    Write-Host ""
    Write-Host "Check logs with: docker-compose logs"
    exit 1
}

Write-Host ""
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host "  Deployment Complete!" -ForegroundColor Cyan
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host ""
Write-Success "ASF TestArena is now running"
Write-Host ""
Write-Host "Access the application at:"
Write-Host "  • Local: http://localhost:5000"
Write-Host "  • Domain: https://testarena.nabd-co.com"
Write-Host ""
Write-Host "Default credentials:"
Write-Host "  • Username: admin"
Write-Host "  • Password: admin123"
Write-Host ""
Write-Warning-Custom "IMPORTANT: Change the default admin password immediately!"
Write-Host ""
Write-Host "Useful commands:"
Write-Host "  • View logs:    docker-compose logs -f"
Write-Host "  • Stop:         docker-compose down"
Write-Host "  • Restart:      docker-compose restart"
Write-Host "  • Status:       docker-compose ps"
Write-Host ""
Write-Host "For help, see START_HERE.md or INDEX.md"
Write-Host ""
