[CmdletBinding()]
param(
    [string]$BaseUrl = '',
    [string]$Username = '',
    [string]$Password = '',
    [switch]$SkipPhpLint,
    [switch]$KeepTestData,
    [switch]$CheckDuplicateSubmit = $true
)

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

$gatePath = Join-Path $PSScriptRoot 'pre_release_gate.ps1'
if (-not (Test-Path -LiteralPath $gatePath)) {
    throw "Pre-release gate script not found: $gatePath"
}

if ([string]::IsNullOrWhiteSpace($BaseUrl)) {
    $BaseUrl = if ([string]::IsNullOrWhiteSpace($env:CCW_BASE_URL)) { 'http://127.0.0.1/CCW' } else { $env:CCW_BASE_URL }
}

if ([string]::IsNullOrWhiteSpace($Username)) {
    $Username = if ([string]::IsNullOrWhiteSpace($env:CCW_SMOKE_USERNAME)) { 'admin' } else { $env:CCW_SMOKE_USERNAME }
}

if ([string]::IsNullOrWhiteSpace($Password)) {
    $Password = if ([string]::IsNullOrWhiteSpace($env:CCW_SMOKE_PASSWORD)) { 'admin123' } else { $env:CCW_SMOKE_PASSWORD }
}

if (-not $SkipPhpLint -and $env:CCW_SKIP_PHP_LINT -eq '1') {
    $SkipPhpLint = $true
}

if (-not $KeepTestData -and $env:CCW_KEEP_TEST_DATA -eq '1') {
    $KeepTestData = $true
}

if ($env:CCW_CHECK_DUPLICATE_SUBMIT -eq '0') {
    $CheckDuplicateSubmit = $false
}

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

if ($SkipPhpLint) {
    $invokeParams['SkipPhpLint'] = $true
}

if ($KeepTestData) {
    $invokeParams['KeepTestData'] = $true
}

$artifactDir = [string]$env:CCW_CI_ARTIFACT_DIR
$transcriptStarted = $false
$transcriptPath = ''
$summaryPath = ''

try {
    if (-not [string]::IsNullOrWhiteSpace($artifactDir)) {
        New-Item -ItemType Directory -Path $artifactDir -Force | Out-Null
        $transcriptPath = Join-Path $artifactDir 'ci-pre-release-gate.log'
        $summaryPath = Join-Path $artifactDir 'ci-pre-release-gate-summary.txt'
        Start-Transcript -Path $transcriptPath -Force | Out-Null
        $transcriptStarted = $true
    }

    Write-Output ("[ci-gate] BaseUrl={0}" -f $BaseUrl)
    Write-Output ("[ci-gate] Username={0}" -f $Username)
    Write-Output ("[ci-gate] SkipPhpLint={0}" -f $SkipPhpLint)
    Write-Output ("[ci-gate] KeepTestData={0}" -f $KeepTestData)
    Write-Output ("[ci-gate] CheckDuplicateSubmit={0}" -f $CheckDuplicateSubmit)

    & $gatePath @invokeParams

    if ($summaryPath -ne '') {
        @(
            'result=passed'
            ('base_url={0}' -f $BaseUrl)
            ('username={0}' -f $Username)
            ('skip_php_lint={0}' -f $SkipPhpLint)
            ('keep_test_data={0}' -f $KeepTestData)
            ('check_duplicate_submit={0}' -f $CheckDuplicateSubmit)
        ) | Set-Content -Path $summaryPath -Encoding utf8
    }
} catch {
    if ($summaryPath -ne '') {
        @(
            'result=failed'
            ('base_url={0}' -f $BaseUrl)
            ('username={0}' -f $Username)
            ('skip_php_lint={0}' -f $SkipPhpLint)
            ('keep_test_data={0}' -f $KeepTestData)
            ('check_duplicate_submit={0}' -f $CheckDuplicateSubmit)
            ('message={0}' -f $_.Exception.Message)
        ) | Set-Content -Path $summaryPath -Encoding utf8
    }
    throw
} finally {
    if ($transcriptStarted) {
        Stop-Transcript | Out-Null
    }
}
