# Verification Script for Historical Data & Backtesting Implementation
# Set console output encoding to UTF-8 for proper emoji display
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Implementation Verification" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# Check for created files
Write-Host "Checking created files..." -ForegroundColor Yellow
$files = @(
    "backend\scripts\validateSchema.js",
    "backend\src\services\performanceMetrics.js",
    "backend\scripts\testBacktester.js",
    "BACKTESTING_IMPLEMENTATION_SUMMARY.md",
    "TESTING_GUIDE.md",
    "IMPLEMENTATION_COMPLETE.md"
)

$allExist = $true
foreach ($file in $files) {
    if (Test-Path $file) {
        Write-Host "  [OK] $file" -ForegroundColor Green
    } else {
        Write-Host "  [MISSING] $file" -ForegroundColor Red
        $allExist = $false
    }
}

Write-Host ""
Write-Host "Checking modified files..." -ForegroundColor Yellow
$modifiedFiles = @(
    "yahhooscript.py",
    "backend\src\services\backtester.js",
    "backend\src\routes\backtest.js",
    "backend\package.json"
)

foreach ($file in $modifiedFiles) {
    if (Test-Path $file) {
        Write-Host "  [OK] $file" -ForegroundColor Green
    } else {
        Write-Host "  [MISSING] $file" -ForegroundColor Red
        $allExist = $false
    }
}

Write-Host ""
Write-Host "Checking yahhooscript.py configuration..." -ForegroundColor Yellow
if (Test-Path "yahhooscript.py") {
    $content = Get-Content "yahhooscript.py" -Raw
    if ($content -match "LOOKBACK_DAYS_INTRADAY = 30") {
        Write-Host "  [OK] LOOKBACK_DAYS_INTRADAY set to 30" -ForegroundColor Green
    } else {
        Write-Host "  [WARNING] LOOKBACK_DAYS_INTRADAY may not be set to 30" -ForegroundColor Yellow
    }
    
    if ($content -match "interval = `"5m`"") {
        Write-Host "  [OK] 5-minute interval logic found" -ForegroundColor Green
    } else {
        Write-Host "  [WARNING] 5-minute interval logic may be missing" -ForegroundColor Yellow
    }
} else {
    Write-Host "  [ERROR] yahhooscript.py not found" -ForegroundColor Red
    $allExist = $false
}

Write-Host ""
Write-Host "Checking backend package.json..." -ForegroundColor Yellow
if (Test-Path "backend\package.json") {
    $pkg = Get-Content "backend\package.json" -Raw | ConvertFrom-Json
    if ($pkg.scripts.'validate-schema') {
        Write-Host "  [OK] validate-schema script found" -ForegroundColor Green
    } else {
        Write-Host "  [WARNING] validate-schema script may be missing" -ForegroundColor Yellow
    }
    if ($pkg.scripts.'test-backtester') {
        Write-Host "  [OK] test-backtester script found" -ForegroundColor Green
    } else {
        Write-Host "  [WARNING] test-backtester script may be missing" -ForegroundColor Yellow
    }
}

Write-Host ""
Write-Host "Checking runtime environments..." -ForegroundColor Yellow
$runtimeOk = $true

# Check Node.js
try {
    $nodeVersion = & node --version 2>&1
    if ($LASTEXITCODE -eq 0 -and $nodeVersion -and -not ($nodeVersion -match "not recognized|not found")) {
        Write-Host "  [OK] Node.js found: $nodeVersion" -ForegroundColor Green
    } else {
        throw "Node.js not found"
    }
} catch {
    Write-Host "  [MISSING] Node.js not found in PATH" -ForegroundColor Red
    $runtimeOk = $false
}

# Check npm
try {
    $npmVersion = & npm --version 2>&1
    if ($LASTEXITCODE -eq 0 -and $npmVersion -and -not ($npmVersion -match "not recognized|not found")) {
        Write-Host "  [OK] npm found: v$npmVersion" -ForegroundColor Green
    } else {
        throw "npm not found"
    }
} catch {
    Write-Host "  [MISSING] npm not found in PATH" -ForegroundColor Red
    $runtimeOk = $false
}

# Check Python
try {
    $pythonVersion = & python --version 2>&1
    if ($LASTEXITCODE -eq 0 -and $pythonVersion -match "Python (\d+\.\d+)") {
        $pyVer = $matches[1]
        Write-Host "  [OK] Python found: $pythonVersion" -ForegroundColor Green
    } else {
        throw "Python not found"
    }
} catch {
    Write-Host "  [MISSING] Python not found in PATH" -ForegroundColor Red
    Write-Host "            (Install from python.org or Microsoft Store)" -ForegroundColor Yellow
    $runtimeOk = $false
}

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
if ($allExist) {
    Write-Host "[SUCCESS] All implementation files verified!" -ForegroundColor Green
    if ($runtimeOk) {
        Write-Host "[SUCCESS] Runtime environments verified!" -ForegroundColor Green
        Write-Host ""
        Write-Host "Ready to proceed with testing:" -ForegroundColor Yellow
        Write-Host "  1. Run: cd backend; npm run validate-schema" -ForegroundColor White
        Write-Host "  2. Run: python yahhooscript.py --only AAPL (to test data pull)" -ForegroundColor White
        Write-Host "  3. Run: cd backend; npm run test-backtester" -ForegroundColor White
    } else {
        Write-Host "[WARNING] Runtime environments not fully available" -ForegroundColor Yellow
        Write-Host ""
        Write-Host "Next steps:" -ForegroundColor Yellow
        Write-Host "  1. Install missing runtime environments:" -ForegroundColor White
        Write-Host "     - Node.js: https://nodejs.org/" -ForegroundColor White
        Write-Host "     - Python: https://www.python.org/ or Microsoft Store" -ForegroundColor White
        Write-Host "  2. Ensure they are added to PATH" -ForegroundColor White
        Write-Host "  3. Restart terminal/IDE after installation" -ForegroundColor White
    }
    Write-Host ""
    Write-Host "See TESTING_GUIDE.md for detailed instructions." -ForegroundColor Cyan
} else {
    Write-Host "[WARNING] Some files are missing. Please check the implementation." -ForegroundColor Yellow
}
Write-Host "========================================" -ForegroundColor Cyan

