# CodeQL SARIF Analysis Script for Windows
# This script analyzes CodeQL SARIF results using Ollama for human-readable explanations

# Configuration
$config = @{
    SarifPath = "python-results.sarif"
    OutputPath = "codeql_analysis_results.txt"
    OllamaEndpoint = "http://localhost:11434/api/generate"
    Model = "deepseek-r1:32b"
}

# Function to check if Ollama is running
function Test-OllamaConnection {
    try {
        $response = Invoke-WebRequest -Uri $config.OllamaEndpoint -Method GET -UseBasicParsing
        return $response.StatusCode -eq 200
    } catch {
        return $false
    }
}

# Function to validate SARIF file
function Test-SarifFile {
    param (
        [string]$Path
    )

    if (-not (Test-Path $Path)) {
        Write-Error "SARIF file not found at: $Path"
        return $false
    }

    try {
        $content = Get-Content $Path -Raw
        $json = $content | ConvertFrom-Json
        return $true
    } catch {
        Write-Error "Invalid SARIF file format: $_"
        return $false
    }
}

# Main script
Write-Host "CodeQL SARIF Analysis Tool" -ForegroundColor Cyan
Write-Host "=========================" -ForegroundColor Cyan

# Check if Ollama is running
Write-Host "Checking Ollama connection..." -NoNewline
if (-not (Test-OllamaConnection)) {
    Write-Host " FAILED" -ForegroundColor Red
    Write-Error "Ollama is not running. Please start Ollama first."
    exit 1
}
Write-Host " OK" -ForegroundColor Green

# Validate SARIF file
Write-Host "Validating SARIF file..." -NoNewline
if (-not (Test-SarifFile $config.SarifPath)) {
    Write-Host " FAILED" -ForegroundColor Red
    exit 1
}
Write-Host " OK" -ForegroundColor Green

# Read SARIF content
try {
    Write-Host "Reading SARIF file..." -NoNewline
    $sarifContent = Get-Content $config.SarifPath -Raw
    Write-Host " OK" -ForegroundColor Green
} catch {
    Write-Host " FAILED" -ForegroundColor Red
    Write-Error "Failed to read SARIF file: $_"
    exit 1
}

# Prepare the prompt
$prompt = @"
Please analyze these security findings from CodeQL analysis.
Focus on:
1. Critical vulnerabilities
2. High priority issues
3. Potential impact
4. Recommended fixes

Here is the analysis data:
$sarifContent
"@

# Prepare the request body
$body = @{
    model = $config.Model
    prompt = $prompt
} | ConvertTo-Json

# Make the request to Ollama and process the streaming response
try {
    Write-Host "Analyzing results with Ollama..." -NoNewline
    $response = ""
    $request = [System.Net.WebRequest]::Create($config.OllamaEndpoint)
    $request.Method = "POST"
    $request.ContentType = "application/json"

    $streamWriter = New-Object System.IO.StreamWriter($request.GetRequestStream())
    $streamWriter.Write($body)
    $streamWriter.Flush()
    $streamWriter.Close()

    $responseStream = $request.GetResponse().GetResponseStream()
    $streamReader = New-Object System.IO.StreamReader($responseStream)

    while (-not $streamReader.EndOfStream) {
        $line = $streamReader.ReadLine()
        if ($line) {
            $jsonResponse = $line | ConvertFrom-Json
            if ($jsonResponse.response) {
                $response += $jsonResponse.response
            }
        }
    }

    # Save the processed response to a file
    $response | Out-File -FilePath $config.OutputPath -Encoding UTF8
    Write-Host " OK" -ForegroundColor Green

    Write-Host "`nAnalysis complete!" -ForegroundColor Green
    Write-Host "Results saved to: $($config.OutputPath)" -ForegroundColor Yellow
    Write-Host "You can view the results by opening: $($config.OutputPath)" -ForegroundColor Yellow
} catch {
    Write-Host " FAILED" -ForegroundColor Red
    Write-Error "Failed to analyze with Ollama: $_"
    exit 1
}
