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

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

$tests = @(
    @{
        Name = 'guest-flow'
        Path = Join-Path $PSScriptRoot 'smoke_quick_service_flow.ps1'
    },
    @{
        Name = 'matched-customer'
        Path = Join-Path $PSScriptRoot 'smoke_quick_service_matched_flow.ps1'
    },
    @{
        Name = 'transfer-slip'
        Path = Join-Path $PSScriptRoot 'smoke_quick_service_transfer_slip.ps1'
    },
    @{
        Name = 'delete-payment'
        Path = Join-Path $PSScriptRoot 'smoke_quick_service_delete_payment.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) {
            $invokeParams['KeepTestData'] = $true
        }

        $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 '== quick-service-suite : summary =='
foreach ($item in $summary) {
    Write-Output ("{0}: {1}" -f $item.Test, $item.Status)
}
