# Text Report Generator Module for HammerDB Benchmark Reports
# Provides functions to generate enhanced plain text reports

function New-TextReport {
    # Generates a comprehensive plain text report
    param([hashtable]$ResultsData)
    
    $nl = [Environment]::NewLine
    $separator = "=" * 80
    $minorSeparator = "-" * 80
    
    $report = ""
    
    # Header
    $report += $separator + $nl
    $report += "HammerDB Benchmark Report" + $nl
    $report += $separator + $nl
    $report += "Configuration: $($ResultsData.metadata.configName)" + $nl
    $report += "Timestamp: $($ResultsData.metadata.timestamp)" + $nl
    $statusText = if ($ResultsData.results.success) { 'SUCCESS' } else { 'FAILED' }
    $report += "Status: $statusText" + $nl
    $report += $separator + $nl + $nl
    
    # Database Configuration
    $report += "DATABASE CONFIGURATION" + $nl
    $report += $minorSeparator + $nl
    $report += "Type: $($ResultsData.database.type)" + $nl
    $report += "Host: $($ResultsData.database.host)" + $nl
    $report += "Port: $($ResultsData.database.port)" + $nl
    if (![string]::IsNullOrEmpty($ResultsData.database.serviceName)) {
        $report += "Service Name: $($ResultsData.database.serviceName)" + $nl
    }
    $report += $nl
    
    # Benchmark Configuration
    $report += "BENCHMARK CONFIGURATION" + $nl
    $report += $minorSeparator + $nl
    $report += "Type: $($ResultsData.benchmark.type)" + $nl + $nl
    
    $report += "Build Settings:" + $nl
    foreach ($key in $ResultsData.benchmark.build.Keys) {
        $report += "  ${key}: $($ResultsData.benchmark.build[$key])" + $nl
    }
    $report += $nl
    
    $report += "Test Settings:" + $nl
    foreach ($key in $ResultsData.benchmark.test.Keys) {
        $report += "  ${key}: $($ResultsData.benchmark.test[$key])" + $nl
    }
    $report += $nl
    
    # Performance Metrics
    $report += "PERFORMANCE METRICS" + $nl
    $report += $minorSeparator + $nl
    
    if ($ResultsData.benchmark.type -eq "TPROC-C") {
        $report += Format-TPROCCMetrics -Metrics $ResultsData.results.metrics
    } elseif ($ResultsData.benchmark.type -eq "TPROC-H") {
        $report += Format-TPROCHMetrics -Metrics $ResultsData.results.metrics
    }
    $report += $nl
    
    # Execution Timeline
    $report += "EXECUTION TIMELINE" + $nl
    $report += $minorSeparator + $nl
    $totalMin = [math]::Round($ResultsData.execution.totalDurationSeconds / 60, 2)
    $report += "Total Duration: $totalMin minutes ($($ResultsData.execution.totalDurationSeconds) seconds)" + $nl + $nl
    
    foreach ($step in $ResultsData.execution.steps) {
        $status = if ($step.success) { "OK" } else { "FAIL" }
        $duration = [math]::Round($step.duration, 2)
        $durationMin = [math]::Round($step.duration / 60, 2)
        $report += "[$status] $($step.name): $duration seconds ($durationMin minutes)" + $nl
    }
    $report += $nl
    
    # Metadata
    $report += "METADATA" + $nl
    $report += $minorSeparator + $nl
    $report += "HammerDB Version: $($ResultsData.metadata.hammerdbVersion)" + $nl
    $report += "Script Version: $($ResultsData.metadata.scriptVersion)" + $nl
    $report += "Platform: $($ResultsData.metadata.platform)" + $nl
    $report += "PowerShell Version: $($ResultsData.metadata.psVersion)" + $nl + $nl
    
    $report += $separator + $nl
    $report += "End of Report" + $nl
    $report += $separator + $nl
    
    return $report
}

function Format-TPROCCMetrics {
    # Formats TPROC-C metrics for text display
    param([hashtable]$Metrics)
    
    $nl = [Environment]::NewLine
    $output = ""
    
    if ($Metrics.virtualUsers.Count -gt 0) {
        $output += "Individual Virtual User Results:" + $nl
        foreach ($vu in $Metrics.virtualUsers) {
            $output += "  Virtual User $($vu.virtualUser): System achieved $($vu.nopm) NOPM from $($vu.tpm) $($vu.database) TPM" + $nl
        }
        $output += $nl
        
        $output += "Consolidated Results:" + $nl
        $output += "  Total NOPM: $($Metrics.consolidatedNOPM)" + $nl
        $output += "  Total TPM: $($Metrics.consolidatedTPM)" + $nl
        $output += "  Virtual Users: $($Metrics.virtualUserCount)" + $nl
        $output += "  Average NOPM per VU: $($Metrics.averageNOPM)" + $nl
        $output += "  Average TPM per VU: $($Metrics.averageTPM)" + $nl
    } else {
        $output += "No metrics data available" + $nl
    }
    
    return $output
}

function Format-TPROCHMetrics {
    # Formats TPROC-H metrics for text display
    param([hashtable]$Metrics)
    
    $nl = [Environment]::NewLine
    $output = ""
    
    if ($Metrics.virtualUsers.Count -gt 0) {
        $output += "Individual Virtual User Results:" + $nl
        foreach ($vu in $Metrics.virtualUsers) {
            $output += "  Virtual User $($vu.virtualUser): Geometric mean of query times returning rows ($($vu.queryCount)) is $($vu.geometricMean)" + $nl
        }
        $output += $nl
        
        $output += "Consolidated Results:" + $nl
        $output += "  Average Geometric Mean: $($Metrics.averageGeometricMean) seconds" + $nl
        $output += "  Query Count: $($Metrics.queryCount)" + $nl
        $output += "  Virtual Users: $($Metrics.virtualUserCount)" + $nl
    } else {
        $output += "No metrics data available" + $nl
    }
    
    return $output
}

Export-ModuleMember -Function @(
    'New-TextReport',
    'Format-TPROCCMetrics',
    'Format-TPROCHMetrics'
)
