# HTML Report Generator Module for HammerDB Benchmark Reports
# Generates professional, self-contained HTML reports with embedded CSS

function New-HtmlReport {
    # Generates a comprehensive HTML report
    param([hashtable]$ResultsData)
    
    $nl = [Environment]::NewLine
    $html = ""
    
    # HTML Header and CSS
    $html += "<!DOCTYPE html>$nl"
    $html += "<html lang='en'>$nl"
    $html += "<head>$nl"
    $html += "<meta charset='UTF-8'>$nl"
    $html += "<meta name='viewport' content='width=device-width, initial-scale=1.0'>$nl"
    $html += "<title>HammerDB Benchmark Report - $($ResultsData.metadata.configName)</title>$nl"
    $html += Get-EmbeddedCss
    $html += "</head>$nl"
    $html += "<body>$nl"
    
    # Report Header
    $html += "<div class='container'>$nl"
    $html += "<div class='header'>$nl"
    $html += "<h1>HammerDB Benchmark Report</h1>$nl"
    $html += "<div class='timestamp'>Generated: $($ResultsData.metadata.timestamp)</div>$nl"
    
    $statusClass = if ($ResultsData.results.success) { 'status-success' } else { 'status-failed' }
    $statusText = if ($ResultsData.results.success) { 'SUCCESS' } else { 'FAILED' }
    $html += "<div class='status $statusClass'>$statusText</div>$nl"
    $html += "</div>$nl"
    
    # Performance Metrics Section (moved to top)
    $html += "<div class='section'>$nl"
    $html += "<h2>Performance Metrics</h2>$nl"
    
    if ($ResultsData.benchmark.type -eq "TPROC-C") {
        $html += Get-TPROCCMetricsHtml -Metrics $ResultsData.results.metrics
    } elseif ($ResultsData.benchmark.type -eq "TPROC-H") {
        $html += Get-TPROCHMetricsHtml -Metrics $ResultsData.results.metrics
    }
    
    $html += "</div>$nl"
    
    # Execution Timeline Section
    $html += Get-TimelineHtml -ResultsData $ResultsData
    
    # Configuration Section (moved to bottom)
    $html += Get-ConfigurationHtml -ResultsData $ResultsData
    
    # Footer
    $html += "<div class='footer'>$nl"
    $html += "<p>HammerDB Version: $($ResultsData.metadata.hammerdbVersion) | "
    $html += "Script Version: $($ResultsData.metadata.scriptVersion) | "
    $html += "Platform: $($ResultsData.metadata.platform) | "
    $html += "PowerShell: $($ResultsData.metadata.psVersion)</p>$nl"
    $html += "</div>$nl"
    $html += "</div>$nl"
    
    $html += "</body>$nl"
    $html += "</html>$nl"
    
    return $html
}

function Get-EmbeddedCss {
    # Returns embedded CSS styles
    $nl = [Environment]::NewLine
    $css = "<style>$nl"
    $css += "* { margin: 0; padding: 0; box-sizing: border-box; }$nl"
    $css += "body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 20px; }$nl"
    $css += ".container { max-width: 1200px; margin: 0 auto; background: white; border-radius: 10px; box-shadow: 0 10px 40px rgba(0,0,0,0.2); overflow: hidden; }$nl"
    $css += ".header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 40px; text-align: center; }$nl"
    $css += ".header h1 { font-size: 2.5em; margin-bottom: 10px; }$nl"
    $css += ".subtitle { font-size: 1.3em; margin-bottom: 10px; opacity: 0.9; }$nl"
    $css += ".timestamp { font-size: 0.9em; opacity: 0.8; }$nl"
    $css += ".status { display: inline-block; margin-top: 15px; padding: 10px 30px; border-radius: 25px; font-weight: bold; font-size: 1.1em; }$nl"
    $css += ".status-success { background: #10b981; color: white; }$nl"
    $css += ".status-failed { background: #ef4444; color: white; }$nl"
    $css += ".section { padding: 30px 40px; border-bottom: 1px solid #e5e7eb; }$nl"
    $css += ".section:last-of-type { border-bottom: none; }$nl"
    $css += ".section h2 { color: #667eea; margin-bottom: 20px; font-size: 1.8em; }$nl"
    $css += ".info-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px; margin-bottom: 20px; }$nl"
    $css += ".info-card { background: #f9fafb; padding: 15px; border-radius: 8px; border-left: 4px solid #667eea; }$nl"
    $css += ".info-label { font-weight: 600; color: #4b5563; display: block; margin-bottom: 5px; }$nl"
    $css += ".info-value { font-size: 1.2em; color: #1f2937; }$nl"
    $css += ".metrics-table { width: 100%; border-collapse: collapse; margin-top: 20px; }$nl"
    $css += ".metrics-table th { background: #667eea; color: white; padding: 12px; text-align: left; font-weight: 600; }$nl"
    $css += ".metrics-table td { padding: 12px; border-bottom: 1px solid #e5e7eb; }$nl"
    $css += ".metrics-table tr:hover { background: #f9fafb; }$nl"
    $css += ".metric-highlight { background: #d1fae5; padding: 20px; border-radius: 8px; margin-bottom: 20px; border-left: 4px solid #10b981; }$nl"
    $css += ".metric-highlight h3 { color: #065f46; margin-bottom: 10px; }$nl"
    $css += ".metric-highlight p { font-size: 1.1em; color: #047857; margin: 5px 0; }$nl"
    $css += ".timeline { margin-top: 20px; }$nl"
    $css += ".timeline-item { background: #f9fafb; padding: 15px; margin-bottom: 10px; border-radius: 8px; border-left: 4px solid #10b981; }$nl"
    $css += ".timeline-item.failed { border-left-color: #ef4444; }$nl"
    $css += ".timeline-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 5px; }$nl"
    $css += ".timeline-title { font-weight: 600; color: #1f2937; }$nl"
    $css += ".timeline-status { padding: 3px 10px; border-radius: 12px; font-size: 0.85em; font-weight: 600; }$nl"
    $css += ".timeline-status.success { background: #d1fae5; color: #065f46; }$nl"
    $css += ".timeline-status.failed { background: #fee2e2; color: #991b1b; }$nl"
    $css += ".timeline-duration { color: #6b7280; font-size: 0.9em; }$nl"
    $css += ".footer { background: #f9fafb; padding: 20px; text-align: center; color: #6b7280; font-size: 0.9em; }$nl"
    $css += "</style>$nl"
    return $css
}

function Get-ConfigurationHtml {
    # Generates configuration section HTML
    param([hashtable]$ResultsData)
    
    $nl = [Environment]::NewLine
    $html = ""
    
    $html += "<div class='section'>$nl"
    $html += "<h2>Configuration</h2>$nl"
    
    # Database Configuration
    $html += "<h3 style='color: #4b5563; margin-bottom: 15px;'>Database</h3>$nl"
    $html += "<div class='info-grid'>$nl"
    
    $html += "<div class='info-card'>$nl"
    $html += "<span class='info-label'>Type</span>$nl"
    $html += "<div class='info-value'>$($ResultsData.database.type)</div>$nl"
    $html += "</div>$nl"
    
    $html += "<div class='info-card'>$nl"
    $html += "<span class='info-label'>Host</span>$nl"
    $html += "<div class='info-value'>$($ResultsData.database.host)</div>$nl"
    $html += "</div>$nl"
    
    $html += "<div class='info-card'>$nl"
    $html += "<span class='info-label'>Port</span>$nl"
    $html += "<div class='info-value'>$($ResultsData.database.port)</div>$nl"
    $html += "</div>$nl"
    
    if (![string]::IsNullOrEmpty($ResultsData.database.serviceName)) {
        $html += "<div class='info-card'>$nl"
        $html += "<span class='info-label'>Service Name</span>$nl"
        $html += "<div class='info-value'>$($ResultsData.database.serviceName)</div>$nl"
        $html += "</div>$nl"
    }
    
    $html += "</div>$nl"
    
    # Benchmark Configuration
    $html += "<h3 style='color: #4b5563; margin: 25px 0 15px 0;'>Benchmark</h3>$nl"
    $html += "<div class='info-grid'>$nl"
    
    $html += "<div class='info-card'>$nl"
    $html += "<span class='info-label'>Type</span>$nl"
    $html += "<div class='info-value'>$($ResultsData.benchmark.type)</div>$nl"
    $html += "</div>$nl"
    
    foreach ($key in $ResultsData.benchmark.build.Keys) {
        $keyLabel = "Build - " + $key
        $html += "<div class='info-card'>$nl"
        $html += "<span class='info-label'>$keyLabel</span>$nl"
        $html += "<div class='info-value'>$($ResultsData.benchmark.build[$key])</div>$nl"
        $html += "</div>$nl"
    }
    
    foreach ($key in $ResultsData.benchmark.test.Keys) {
        $keyLabel = "Test - " + $key
        $html += "<div class='info-card'>$nl"
        $html += "<span class='info-label'>$keyLabel</span>$nl"
        $html += "<div class='info-value'>$($ResultsData.benchmark.test[$key])</div>$nl"
        $html += "</div>$nl"
    }
    
    $html += "</div>$nl"
    $html += "</div>$nl"
    
    return $html
}

function Get-TPROCCMetricsHtml {
    # Generates TPROC-C metrics HTML
    param([hashtable]$Metrics)
    
    $nl = [Environment]::NewLine
    $html = ""
    
    if ($Metrics.virtualUsers.Count -gt 0) {
        # Check if we have individual VU results or just consolidated
        $hasIndividualVUs = ($Metrics.virtualUsers | Where-Object { $_.virtualUser -gt 0 }).Count -gt 0
        
        if ($hasIndividualVUs) {
            # Consolidated Results (moved to top)
            $html += "<div class='metric-highlight'>$nl"
            $html += "<h3>Consolidated Results</h3>$nl"
            $html += "<p><strong>Total NOPM:</strong> $($Metrics.consolidatedNOPM)</p>$nl"
            $html += "<p><strong>Total TPM:</strong> $($Metrics.consolidatedTPM)</p>$nl"
            $html += "<p><strong>Average NOPM per VU:</strong> $($Metrics.averageNOPM)</p>$nl"
            $html += "<p><strong>Average TPM per VU:</strong> $($Metrics.averageTPM)</p>$nl"
            $html += "<p><strong>Virtual Users:</strong> $($Metrics.virtualUserCount)</p>$nl"
            $html += "</div>$nl"
            
            # Individual VU Results Table
            $html += "<table class='metrics-table'>$nl"
            $html += "<thead>$nl"
            $html += "<tr>$nl"
            $html += "<th>Virtual User</th>$nl"
            $html += "<th>NOPM</th>$nl"
            $html += "<th>TPM</th>$nl"
            $html += "<th>Database</th>$nl"
            $html += "</tr>$nl"
            $html += "</thead>$nl"
            $html += "<tbody>$nl"
            
            foreach ($vu in $Metrics.virtualUsers) {
                $html += "<tr>$nl"
                $html += "<td>Virtual User $($vu.virtualUser)</td>$nl"
                $html += "<td>$($vu.nopm)</td>$nl"
                $html += "<td>$($vu.tpm)</td>$nl"
                $html += "<td>$($vu.database)</td>$nl"
                $html += "</tr>$nl"
            }
            
            $html += "</tbody>$nl"
            $html += "</table>$nl"
        } else {
            # Only consolidated result available
            $html += "<div class='metric-highlight'>$nl"
            $html += "<h3>Performance Results</h3>$nl"
            $html += "<p><strong>System achieved $($Metrics.consolidatedNOPM) NOPM from $($Metrics.consolidatedTPM) $($Metrics.databaseType) TPM</strong></p>$nl"
            $html += "</div>$nl"
        }
    } else {
        $html += "<p>No metrics data available</p>$nl"
    }
    
    return $html
}

function Get-TPROCHMetricsHtml {
    # Generates TPROC-H metrics HTML
    param([hashtable]$Metrics)
    
    $nl = [Environment]::NewLine
    $html = ""
    
    if ($Metrics.virtualUsers.Count -gt 0) {
        # Consolidated Results (moved to top)
        $html += "<div class='metric-highlight'>$nl"
        $html += "<h3>Consolidated Results</h3>$nl"
        $html += "<p><strong>Average of all virtual users:</strong> $($Metrics.averageGeometricMean) seconds</p>$nl"
        $html += "<p><strong>Geometric mean of query times returning rows ($($Metrics.queryCount)):</strong> $($Metrics.averageGeometricMean) seconds</p>$nl"
        $html += "<p><strong>Virtual Users:</strong> $($Metrics.virtualUserCount)</p>$nl"
        $html += "</div>$nl"
        
        # Individual VU Results Table
        $html += "<table class='metrics-table'>$nl"
        $html += "<thead>$nl"
        $html += "<tr>$nl"
        $html += "<th>Virtual User</th>$nl"
        $html += "<th>Geometric Mean (seconds)</th>$nl"
        $html += "<th>Query Count</th>$nl"
        $html += "</tr>$nl"
        $html += "</thead>$nl"
        $html += "<tbody>$nl"
        
        foreach ($vu in $Metrics.virtualUsers) {
            $html += "<tr>$nl"
            $html += "<td>Virtual User $($vu.virtualUser)</td>$nl"
            $html += "<td>$($vu.geometricMean)</td>$nl"
            $html += "<td>$($vu.queryCount)</td>$nl"
            $html += "</tr>$nl"
        }
        
        $html += "</tbody>$nl"
        $html += "</table>$nl"
    } else {
        $html += "<p>No metrics data available</p>$nl"
    }
    
    return $html
}

function Get-TimelineHtml {
    # Generates execution timeline HTML
    param([hashtable]$ResultsData)
    
    $nl = [Environment]::NewLine
    $html = ""
    
    $html += "<div class='section'>$nl"
    $html += "<h2>Execution Timeline</h2>$nl"
    
    $totalMin = [math]::Round($ResultsData.execution.totalDurationSeconds / 60, 2)
    $html += "<p style='margin-bottom: 20px; color: #4b5563;'><strong>Total Duration:</strong> $totalMin minutes ($($ResultsData.execution.totalDurationSeconds) seconds)</p>$nl"
    
    $html += "<div class='timeline'>$nl"
    
    foreach ($step in $ResultsData.execution.steps) {
        $itemClass = if ($step.success) { '' } else { 'failed' }
        $statusClass = if ($step.success) { 'success' } else { 'failed' }
        $statusText = if ($step.success) { 'SUCCESS' } else { 'FAILED' }
        
        $duration = [math]::Round($step.duration, 2)
        $durationMin = [math]::Round($step.duration / 60, 2)
        $durationText = "$duration seconds ($durationMin minutes)"
        
        $html += "<div class='timeline-item $itemClass'>$nl"
        $html += "<div class='timeline-header'>$nl"
        $html += "<span class='timeline-title'>$($step.name)</span>$nl"
        $html += "<span class='timeline-status $statusClass'>$statusText</span>$nl"
        $html += "</div>$nl"
        $html += "<div class='timeline-duration'>$durationText</div>$nl"
        $html += "</div>$nl"
    }
    
    $html += "</div>$nl"
    $html += "</div>$nl"
    
    return $html
}

Export-ModuleMember -Function @(
    'New-HtmlReport',
    'Get-TPROCCMetricsHtml',
    'Get-TPROCHMetricsHtml',
    'Get-ConfigurationHtml',
    'Get-TimelineHtml'
)
