[CmdletBinding()]
param(
    [string]$BaseUrl = 'http://127.0.0.1/CCW',
    [string]$Username = 'admin',
    [string]$Password = 'admin123',
    [switch]$KeepTestData,
    [switch]$CheckDuplicateSubmit = $true
)

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

$tests = @(
    @{
        Name = 'order-flow'
        Path = Join-Path $PSScriptRoot 'smoke_order_flow.ps1'
        SupportsKeepTestData = $true
        SupportsCheckDuplicateSubmit = $true
    },
    @{
        Name = 'customer-search'
        Path = Join-Path $PSScriptRoot 'smoke_customer_search.ps1'
    },
    @{
        Name = 'booking-deposit'
        Path = Join-Path $PSScriptRoot 'smoke_booking_deposit_flow.ps1'
        SupportsKeepTestData = $true
    },
    @{
        Name = 'quick-service'
        Path = Join-Path $PSScriptRoot 'smoke_quick_service_suite.ps1'
        SupportsKeepTestData = $true
    },
    @{
        Name = 'branch-scope'
        Path = Join-Path $PSScriptRoot 'smoke_branch_scope_flow.ps1'
        SupportsKeepTestData = $true
    },
    @{
        Name = 'admin-support'
        Path = Join-Path $PSScriptRoot 'smoke_admin_support_suite.ps1'
    }
)

$summary = New-Object System.Collections.Generic.List[object]

foreach ($test in $tests) {
    $name = [string] $test.Name
    $path = [string] $test.Path

    if (-not (Test-Path -LiteralPath $path)) {
        throw "Smoke script not found: $path"
    }

    Write-Output ("== {0} : start ==" -f $name)

    try {
        $invokeParams = @{
            BaseUrl = $BaseUrl
            Username = $Username
            Password = $Password
        }

        if ($KeepTestData -and $test.ContainsKey('SupportsKeepTestData') -and ($test['SupportsKeepTestData'] -eq $true)) {
            $invokeParams['KeepTestData'] = $true
        }

        if ($test.ContainsKey('SupportsCheckDuplicateSubmit') -and ($test['SupportsCheckDuplicateSubmit'] -eq $true)) {
            $invokeParams['CheckDuplicateSubmit'] = $CheckDuplicateSubmit
        }

        $output = & $path @invokeParams 2>&1
        foreach ($line in $output) {
            Write-Output ("[{0}] {1}" -f $name, [string] $line)
        }

        $summary.Add([pscustomobject]@{
            Test = $name
            Status = 'passed'
        }) | Out-Null

        Write-Output ("== {0} : passed ==" -f $name)
    } catch {
        $summary.Add([pscustomobject]@{
            Test = $name
            Status = 'failed'
            Message = $_.Exception.Message
        }) | Out-Null

        Write-Output ("== {0} : failed ==" -f $name)
        throw
    }
}

Write-Output '== system-suite : summary =='
foreach ($item in $summary) {
    Write-Output ("{0}: {1}" -f $item.Test, $item.Status)
}
