# Quick Start Script for EasyStream Continuous Delivery
# This script provides easy access to CD functionality

param(
    [Parameter(Position=0)]
    [ValidateSet("start", "watch", "status", "stop", "once", "help")]
    [string]$Action = "help",

    [int]$Interval = 300
)

$RepoPath = $PSScriptRoot
$AutoDeployScript = Join-Path $RepoPath "auto-deploy.ps1"

function Show-Help {
    Write-Host ""
    Write-Host "EasyStream Continuous Delivery Manager" -ForegroundColor Cyan
    Write-Host "======================================" -ForegroundColor Cyan
    Write-Host ""
    Write-Host "USAGE:" -ForegroundColor Yellow
    Write-Host "  .\start-cd.ps1 <action> [options]"
    Write-Host ""
    Write-Host "ACTIONS:" -ForegroundColor Yellow
    Write-Host "  start              Start CD with timer (default: 5 min intervals)"
    Write-Host "  watch              Start CD with file watcher (auto-detect changes)"
    Write-Host "  once               Run one-time commit and push"
    Write-Host "  status             Check current git status"
    Write-Host "  stop               Stop all running CD processes"
    Write-Host "  help               Show this help message"
    Write-Host ""
    Write-Host "OPTIONS:" -ForegroundColor Yellow
    Write-Host "  -Interval <sec>    Set interval in seconds (default: 300)"
    Write-Host ""
    Write-Host "EXAMPLES:" -ForegroundColor Yellow
    Write-Host "  .\start-cd.ps1 watch              # Start file watcher mode"
    Write-Host "  .\start-cd.ps1 start -Interval 60 # Check every 1 minute"
    Write-Host "  .\start-cd.ps1 once               # Commit and push now"
    Write-Host ""
    Write-Host "RECOMMENDED:" -ForegroundColor Green
    Write-Host "  Use 'watch' mode for automatic detection of file changes"
    Write-Host ""
}

function Start-CDService {
    Write-Host "Starting Continuous Delivery (Timer Mode)..." -ForegroundColor Green
    Write-Host "Interval: $Interval seconds" -ForegroundColor Cyan
    Write-Host "Press Ctrl+C to stop" -ForegroundColor Yellow
    Write-Host ""
    & $AutoDeployScript -IntervalSeconds $Interval -Verbose
}

function Start-WatchService {
    Write-Host "Starting Continuous Delivery (File Watcher Mode)..." -ForegroundColor Green
    Write-Host "Changes will auto-commit 30 seconds after last modification" -ForegroundColor Cyan
    Write-Host "Press Ctrl+C to stop" -ForegroundColor Yellow
    Write-Host ""
    & $AutoDeployScript -WatchMode -Verbose
}

function Invoke-OnceCommit {
    Write-Host "Running one-time commit and push..." -ForegroundColor Green
    Write-Host ""

    Push-Location $RepoPath
    try {
        # Check for changes
        $status = git status --porcelain
        if (-not $status) {
            Write-Host "No changes to commit" -ForegroundColor Yellow
            return
        }

        Write-Host "Changes detected:" -ForegroundColor Cyan
        git status --short

        # Confirm
        $response = Read-Host "`nProceed with commit and push? (y/n)"
        if ($response -ne 'y') {
            Write-Host "Cancelled" -ForegroundColor Yellow
            return
        }

        # Stage all
        Write-Host "`nStaging all changes..." -ForegroundColor Cyan
        git add -A

        # Commit
        $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        $message = "manual: Update at $timestamp`n`n🤖 Generated with Claude Code Continuous Delivery`n`nCo-Authored-By: Claude <noreply@anthropic.com>"

        Write-Host "Creating commit..." -ForegroundColor Cyan
        git commit -m $message

        if ($LASTEXITCODE -eq 0) {
            Write-Host "✓ Commit successful" -ForegroundColor Green

            # Push
            Write-Host "Pushing to GitHub..." -ForegroundColor Cyan
            git push origin dev

            if ($LASTEXITCODE -eq 0) {
                Write-Host "✓ Push successful" -ForegroundColor Green
            } else {
                Write-Host "✗ Push failed" -ForegroundColor Red
            }
        } else {
            Write-Host "✗ Commit failed" -ForegroundColor Red
        }
    } finally {
        Pop-Location
    }
}

function Show-Status {
    Push-Location $RepoPath
    try {
        Write-Host "Git Status:" -ForegroundColor Cyan
        Write-Host ""
        git status

        Write-Host "`nLast 5 commits:" -ForegroundColor Cyan
        git log --oneline -5

        Write-Host "`nRemote status:" -ForegroundColor Cyan
        git remote -v
    } finally {
        Pop-Location
    }
}

function Stop-CDService {
    Write-Host "Stopping Continuous Delivery services..." -ForegroundColor Yellow

    $processes = Get-Process powershell -ErrorAction SilentlyContinue |
        Where-Object { $_.CommandLine -like "*auto-deploy.ps1*" }

    if ($processes) {
        $processes | Stop-Process -Force
        Write-Host "✓ Stopped $($processes.Count) CD process(es)" -ForegroundColor Green
    } else {
        Write-Host "No running CD processes found" -ForegroundColor Yellow
    }
}

# Execute action
switch ($Action) {
    "start" { Start-CDService }
    "watch" { Start-WatchService }
    "once" { Invoke-OnceCommit }
    "status" { Show-Status }
    "stop" { Stop-CDService }
    "help" { Show-Help }
    default { Show-Help }
}
