# CSV Export Generator Module for HammerDB Benchmark Reports
# Provides functions to export benchmark results to CSV format

function Export-SummaryToCsv {
    # Exports high-level summary to CSV
    param(
        [hashtable]$ResultsData,
        [string]$OutputPath
    )
    
    $csvData = [PSCustomObject]@{
        ConfigName = $ResultsData.metadata.configName
        Timestamp = $ResultsData.metadata.timestamp
        DatabaseType = $ResultsData.database.type
        DatabaseHost = $ResultsData.database.host
        DatabasePort = $ResultsData.database.port
        BenchmarkType = $ResultsData.benchmark.type
        TotalDurationSeconds = $ResultsData.execution.totalDurationSeconds
        TotalDurationMinutes = [math]::Round($ResultsData.execution.totalDurationSeconds / 60, 2)
        Success = $ResultsData.results.success
        HammerDBVersion = $ResultsData.metadata.hammerdbVersion
        Platform = $ResultsData.metadata.platform
    }
    
    # Add benchmark-specific metrics
    if ($ResultsData.benchmark.type -eq "TPROC-C") {
        $csvData | Add-Member -NotePropertyName "ConsolidatedNOPM" -NotePropertyValue $ResultsData.results.metrics.consolidatedNOPM
        $csvData | Add-Member -NotePropertyName "ConsolidatedTPM" -NotePropertyValue $ResultsData.results.metrics.consolidatedTPM
        $csvData | Add-Member -NotePropertyName "AverageNOPM" -NotePropertyValue $ResultsData.results.metrics.averageNOPM
        $csvData | Add-Member -NotePropertyName "AverageTPM" -NotePropertyValue $ResultsData.results.metrics.averageTPM
    } elseif ($ResultsData.benchmark.type -eq "TPROC-H") {
        $csvData | Add-Member -NotePropertyName "AverageGeometricMean" -NotePropertyValue $ResultsData.results.metrics.averageGeometricMean
        $csvData | Add-Member -NotePropertyName "QueryCount" -NotePropertyValue $ResultsData.results.metrics.queryCount
    }
    
    $csvData | Export-Csv -Path $OutputPath -NoTypeInformation -Encoding UTF8
    
    Write-Host "Summary CSV exported to: $OutputPath" -ForegroundColor Green
}

function Export-VirtualUsersToCsv {
    # Exports per-virtual-user metrics to CSV
    param(
        [hashtable]$ResultsData,
        [string]$OutputPath
    )
    
    $csvData = @()
    
    if ($ResultsData.benchmark.type -eq "TPROC-C") {
        foreach ($vu in $ResultsData.results.metrics.virtualUsers) {
            $csvData += [PSCustomObject]@{
                ConfigName = $ResultsData.metadata.configName
                Timestamp = $ResultsData.metadata.timestamp
                BenchmarkType = "TPROC-C"
                VirtualUser = $vu.virtualUser
                NOPM = $vu.nopm
                TPM = $vu.tpm
                Database = $vu.database
            }
        }
    } elseif ($ResultsData.benchmark.type -eq "TPROC-H") {
        foreach ($vu in $ResultsData.results.metrics.virtualUsers) {
            $csvData += [PSCustomObject]@{
                ConfigName = $ResultsData.metadata.configName
                Timestamp = $ResultsData.metadata.timestamp
                BenchmarkType = "TPROC-H"
                VirtualUser = $vu.virtualUser
                GeometricMean = $vu.geometricMean
                QueryCount = $vu.queryCount
            }
        }
    }
    
    if ($csvData.Count -gt 0) {
        $csvData | Export-Csv -Path $OutputPath -NoTypeInformation -Encoding UTF8
        Write-Host "Virtual Users CSV exported to: $OutputPath" -ForegroundColor Green
    }
}

function Export-ExecutionTimelineToCsv {
    # Exports execution timeline to CSV
    param(
        [hashtable]$ResultsData,
        [string]$OutputPath
    )
    
    $csvData = @()
    
    foreach ($step in $ResultsData.execution.steps) {
        $csvData += [PSCustomObject]@{
            ConfigName = $ResultsData.metadata.configName
            Timestamp = $ResultsData.metadata.timestamp
            StepName = $step.name
            StartTime = $step.startTime
            EndTime = $step.endTime
            DurationSeconds = [math]::Round($step.duration, 2)
            DurationMinutes = [math]::Round($step.duration / 60, 2)
            Success = $step.success
        }
    }
    
    if ($csvData.Count -gt 0) {
        $csvData | Export-Csv -Path $OutputPath -NoTypeInformation -Encoding UTF8
        Write-Host "Execution Timeline CSV exported to: $OutputPath" -ForegroundColor Green
    }
}

Export-ModuleMember -Function @(
    'Export-SummaryToCsv',
    'Export-VirtualUsersToCsv',
    'Export-ExecutionTimelineToCsv'
)
