#!/usr/bin/env pwsh
# ColaFlow Development Environment Startup Script
# This script starts both backend API and frontend web application

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  ColaFlow Development Environment" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# Get the script directory (project root)
$ProjectRoot = $PSScriptRoot

# Function to check if a port is in use
function Test-Port {
    param (
        [int]$Port
    )
    $connection = Test-NetConnection -ComputerName localhost -Port $Port -WarningAction SilentlyContinue -InformationLevel Quiet
    return $connection
}

# Function to start backend
function Start-Backend {
    Write-Host "[Backend] Starting ColaFlow API..." -ForegroundColor Yellow

    # Check if backend port is already in use
    if (Test-Port -Port 5000) {
        Write-Host "[Backend] Port 5000 is already in use. Backend may already be running." -ForegroundColor Green
        return
    }

    # Start backend in a new window
    $backendPath = Join-Path $ProjectRoot "colaflow-api"
    $backendProject = Join-Path $backendPath "src\ColaFlow.API\ColaFlow.API.csproj"

    if (-not (Test-Path $backendProject)) {
        Write-Host "[Backend] ERROR: Backend project not found at $backendProject" -ForegroundColor Red
        return
    }

    Write-Host "[Backend] Starting at $backendPath" -ForegroundColor Gray
    Start-Process pwsh -ArgumentList "-NoExit", "-Command", "cd '$backendPath'; dotnet run --project '$backendProject'" -WindowStyle Normal

    Write-Host "[Backend] Waiting for API to start..." -ForegroundColor Yellow
    Start-Sleep -Seconds 5

    if (Test-Port -Port 5000) {
        Write-Host "[Backend] API started successfully at http://localhost:5000" -ForegroundColor Green
        Write-Host "[Backend] Swagger UI: http://localhost:5000/swagger" -ForegroundColor Green
    } else {
        Write-Host "[Backend] API is starting... Please check the backend window for status" -ForegroundColor Yellow
    }
}

# Function to start frontend
function Start-Frontend {
    Write-Host "[Frontend] Starting ColaFlow Web..." -ForegroundColor Yellow

    # Check if frontend port is already in use
    if (Test-Port -Port 3000) {
        Write-Host "[Frontend] Port 3000 is already in use. Frontend may already be running." -ForegroundColor Green
        Write-Host "[Frontend] Web UI: http://localhost:3000" -ForegroundColor Green
        return
    }

    # Start frontend in a new window
    $frontendPath = Join-Path $ProjectRoot "colaflow-web"

    if (-not (Test-Path $frontendPath)) {
        Write-Host "[Frontend] ERROR: Frontend directory not found at $frontendPath" -ForegroundColor Red
        return
    }

    # Check if node_modules exists
    $nodeModules = Join-Path $frontendPath "node_modules"
    if (-not (Test-Path $nodeModules)) {
        Write-Host "[Frontend] node_modules not found. Installing dependencies first..." -ForegroundColor Yellow
        Push-Location $frontendPath
        npm install
        Pop-Location
    }

    Write-Host "[Frontend] Starting at $frontendPath" -ForegroundColor Gray
    Start-Process pwsh -ArgumentList "-NoExit", "-Command", "cd '$frontendPath'; npm run dev" -WindowStyle Normal

    Write-Host "[Frontend] Waiting for Web to start..." -ForegroundColor Yellow
    Start-Sleep -Seconds 5

    if (Test-Port -Port 3000) {
        Write-Host "[Frontend] Web started successfully at http://localhost:3000" -ForegroundColor Green
    } else {
        Write-Host "[Frontend] Web is starting... Please check the frontend window for status" -ForegroundColor Yellow
    }
}

# Main execution
Write-Host "Starting development environment..." -ForegroundColor Cyan
Write-Host ""

# Start backend first
Start-Backend
Write-Host ""

# Start frontend
Start-Frontend
Write-Host ""

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  Development Environment Started" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Services:" -ForegroundColor White
Write-Host "  - Backend API: http://localhost:5000" -ForegroundColor White
Write-Host "  - Swagger UI:  http://localhost:5000/swagger" -ForegroundColor White
Write-Host "  - Frontend:    http://localhost:3000" -ForegroundColor White
Write-Host ""
Write-Host "Press Ctrl+C to exit this script." -ForegroundColor Gray
Write-Host "Note: Backend and Frontend are running in separate windows." -ForegroundColor Gray
Write-Host ""

# Keep the script running
try {
    while ($true) {
        Start-Sleep -Seconds 1
    }
} finally {
    Write-Host "Goodbye!" -ForegroundColor Cyan
}
