# HM ITAM Server Start Script
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

Write-Host "============================================" -ForegroundColor Cyan
Write-Host "   HM ITAM System Start" -ForegroundColor Cyan
Write-Host "============================================" -ForegroundColor Cyan
Write-Host ""

Write-Host "[INFO] Checking Node.js and npm..."
if (-not (Get-Command node -ErrorAction SilentlyContinue)) {
    Write-Host "[ERROR] Node.js not found." -ForegroundColor Red
    Read-Host "Press Enter to exit"
    exit
}

if (-not (Test-Path "node_modules")) {
    Write-Host "[INFO] Installing dependencies..."
    npm install
}

Write-Host "[INFO] Checking ports..."
$backendPort = 3000
$frontendPort = 8080

if (Get-NetTCPConnection -LocalPort $backendPort -ErrorAction SilentlyContinue) {
    Write-Host "[WARNING] Port $backendPort [Backend] is already in use." -ForegroundColor Yellow
}

if (Get-NetTCPConnection -LocalPort $frontendPort -ErrorAction SilentlyContinue) {
    Write-Host "[WARNING] Port $frontendPort [Frontend] is already in use." -ForegroundColor Yellow
}

Write-Host ""
Write-Host "[INFO] Starting Backend [Port: 3000]..."
Start-Process cmd -ArgumentList "/k npm run server"

Write-Host "[INFO] Starting Frontend [Port: 8080]..."
Start-Process cmd -ArgumentList "/k npm run dev"

Write-Host ""
Write-Host "============================================" -ForegroundColor Green
Write-Host " [OK] Server commands issued successfully." -ForegroundColor Green
Write-Host " [INFO] Please check the new windows for logs."
Write-Host "============================================" -ForegroundColor Green
Write-Host ""

Read-Host "Press Enter to continue..."
