# Docker Helper Script for MyClub Project
# Provides common Docker operations with optimizations

param(
    [Parameter(Position=0)]
    [ValidateSet('start', 'stop', 'restart', 'build', 'logs', 'stats', 'clean', 'reset')]
    [string]$Command = 'start',
    
    [Parameter(Position=1)]
    [string]$Service = ''
)

# Enable BuildKit for better performance
$env:DOCKER_BUILDKIT = 1
$env:COMPOSE_DOCKER_CLI_BUILD = 1

Write-Host "MyClub Docker Helper" -ForegroundColor Cyan
Write-Host "===================" -ForegroundColor Cyan
Write-Host ""

switch ($Command) {
    'start' {
        Write-Host "Starting services..." -ForegroundColor Green
        if ($Service) {
            docker-compose up -d $Service
        } else {
            docker-compose up -d
        }
        Write-Host ""
        Write-Host "Services started! Access:" -ForegroundColor Green
        Write-Host "  Frontend: http://localhost:3000" -ForegroundColor Yellow
        Write-Host "  Backend:  http://localhost:8080" -ForegroundColor Yellow
        Write-Host "  Database: localhost:5432" -ForegroundColor Yellow
    }
    
    'stop' {
        Write-Host "Stopping services..." -ForegroundColor Yellow
        if ($Service) {
            docker-compose stop $Service
        } else {
            docker-compose stop
        }
    }
    
    'restart' {
        Write-Host "Restarting services..." -ForegroundColor Yellow
        if ($Service) {
            docker-compose restart $Service
        } else {
            docker-compose restart
        }
    }
    
    'build' {
        Write-Host "Building services with cache optimization..." -ForegroundColor Green
        Write-Host "(This may take a few minutes on first build)" -ForegroundColor Gray
        if ($Service) {
            docker-compose build $Service
        } else {
            docker-compose build
        }
        Write-Host ""
        Write-Host "Build complete! Run './docker-helper.ps1 start' to launch." -ForegroundColor Green
    }
    
    'logs' {
        Write-Host "Showing logs (Ctrl+C to exit)..." -ForegroundColor Cyan
        if ($Service) {
            docker-compose logs -f --tail=100 $Service
        } else {
            docker-compose logs -f --tail=100
        }
    }
    
    'stats' {
        Write-Host "Resource usage statistics:" -ForegroundColor Cyan
        Write-Host ""
        docker stats --no-stream myclub-backend myclub-frontend myclub-db
        Write-Host ""
        Write-Host "Tip: Run 'docker stats' for live monitoring" -ForegroundColor Gray
    }
    
    'clean' {
        Write-Host "Cleaning up Docker resources..." -ForegroundColor Yellow
        Write-Host ""
        
        $confirm = Read-Host "This will remove stopped containers, unused networks, and dangling images. Continue? (y/N)"
        if ($confirm -eq 'y' -or $confirm -eq 'Y') {
            docker-compose down
            docker system prune -f
            Write-Host ""
            Write-Host "Cleanup complete!" -ForegroundColor Green
        } else {
            Write-Host "Cleanup cancelled." -ForegroundColor Gray
        }
    }
    
    'reset' {
        Write-Host "FULL RESET - This will delete ALL data including database!" -ForegroundColor Red
        Write-Host ""
        
        $confirm = Read-Host "Are you absolutely sure? Type 'yes' to continue"
        if ($confirm -eq 'yes') {
            docker-compose down -v
            docker system prune -af
            docker builder prune -af
            Write-Host ""
            Write-Host "Full reset complete! Run './docker-helper.ps1 build' to rebuild." -ForegroundColor Green
        } else {
            Write-Host "Reset cancelled." -ForegroundColor Gray
        }
    }
    
    default {
        Write-Host "Unknown command: $Command" -ForegroundColor Red
        Write-Host ""
        Write-Host "Usage: ./docker-helper.ps1 [command] [service]" -ForegroundColor White
        Write-Host ""
        Write-Host "Commands:" -ForegroundColor Cyan
        Write-Host "  start    - Start all services (default)" -ForegroundColor White
        Write-Host "  stop     - Stop all services" -ForegroundColor White
        Write-Host "  restart  - Restart all services" -ForegroundColor White
        Write-Host "  build    - Build/rebuild services" -ForegroundColor White
        Write-Host "  logs     - View service logs" -ForegroundColor White
        Write-Host "  stats    - Show resource usage" -ForegroundColor White
        Write-Host "  clean    - Clean up Docker resources" -ForegroundColor White
        Write-Host "  reset    - Full reset (deletes all data!)" -ForegroundColor White
        Write-Host ""
        Write-Host "Examples:" -ForegroundColor Cyan
        Write-Host "  ./docker-helper.ps1 start" -ForegroundColor Gray
        Write-Host "  ./docker-helper.ps1 logs backend" -ForegroundColor Gray
        Write-Host "  ./docker-helper.ps1 restart frontend" -ForegroundColor Gray
    }
}
