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

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

$tests = @(
    @{
        Name = 'member-employee'
        Path = Join-Path $PSScriptRoot 'smoke_admin_support_flow.ps1'
    },
    @{
        Name = 'service-management'
        Path = Join-Path $PSScriptRoot 'smoke_service_management_flow.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 {
        $output = & $path -BaseUrl $BaseUrl -Username $Username -Password $Password 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 '== admin-support-suite : summary =='
foreach ($item in $summary) {
    Write-Output ("{0}: {1}" -f $item.Test, $item.Status)
}
