# Results Data Structure Module for HammerDB Benchmark Reporting
# Provides functions to create and manage the benchmark results data structure

function New-BenchmarkResults {
    # Creates a new benchmark results data structure
    
    return @{
        metadata = @{
            configName = ""
            timestamp = ""
            timestampUtc = ""
            hammerdbVersion = "5.0"
            scriptVersion = "1.0"
            platform = if ($PSVersionTable.PSObject.Properties.Name -contains 'Platform') { 
                $PSVersionTable.Platform 
            } else { 
                "Windows" 
            }
            psVersion = $PSVersionTable.PSVersion.ToString()
        }
        database = @{
            type = ""
            host = ""
            port = 0
            serviceName = ""
            version = ""
        }
        benchmark = @{
            type = ""
            build = @{}
            test = @{}
        }
        execution = @{
            startTime = ""
            endTime = ""
            totalDurationSeconds = 0
            steps = @()
        }
        results = @{
            success = $false
            metrics = @{}
            errors = @()
        }
    }
}

function Add-ExecutionStep {
    # Adds an execution step to the results data
    param(
        [hashtable]$ResultsData,
        [string]$StepName,
        [double]$Duration,
        [string]$StartTime,
        [string]$EndTime,
        [bool]$Success
    )
    
    $step = @{
        name = $StepName
        duration = $Duration
        startTime = $StartTime
        endTime = $EndTime
        success = $Success
    }
    
    $ResultsData.execution.steps += $step
}

function Set-DatabaseInfo {
    # Sets database information in the results data
    param(
        [hashtable]$ResultsData,
        [PSCustomObject]$DbConfig
    )
    
    $ResultsData.database.type = $DbConfig.type
    $ResultsData.database.host = $DbConfig.host
    $ResultsData.database.port = $DbConfig.port
    
    if ($DbConfig.PSObject.Properties.Name -contains 'serviceName') {
        $ResultsData.database.serviceName = $DbConfig.serviceName
    }
}

function Set-BenchmarkInfo {
    # Sets benchmark configuration in the results data
    param(
        [hashtable]$ResultsData,
        [PSCustomObject]$DbConfig
    )
    
    $ResultsData.benchmark.type = $DbConfig.benchmark.type
    
    # Copy build settings
    $ResultsData.benchmark.build = @{}
    foreach ($prop in $DbConfig.benchmark.build.PSObject.Properties) {
        $ResultsData.benchmark.build[$prop.Name] = $prop.Value
    }
    
    # Copy test settings
    $ResultsData.benchmark.test = @{}
    foreach ($prop in $DbConfig.benchmark.test.PSObject.Properties) {
        $ResultsData.benchmark.test[$prop.Name] = $prop.Value
    }
}

Export-ModuleMember -Function @(
    'New-BenchmarkResults',
    'Add-ExecutionStep',
    'Set-DatabaseInfo',
    'Set-BenchmarkInfo'
)
