param(
    [string]$ServerHost = '192.168.0.185',
    [string]$Username = 'sevenhill',
    [Parameter(Mandatory = $true)]
    [string]$Password,
    [string]$ArgusBaseUrl = 'https://argus.kusoft.xyz',
    [string]$ArgusInternalBaseUrl = 'unused',
    [string]$ArgusEnvPath = 'unused',
    [string]$Slug = 'ikar-android',
    [string]$Notes = 'Android client update for Ikar',
    [string]$AppName = 'Ikar',
    [string]$Summary = 'Standalone Android client for Ikar on Kotlin and Jetpack Compose.',
    [string]$Description = 'Standalone Android client on Kotlin and Jetpack Compose, prepared as a staged replacement for the current MAUI client.',
    [string]$HomepageUrl = 'https://ikar.kusoft.xyz',
    [string]$RepositoryUrl = 'https://git.kusoft.xyz/sevenhill/Ikar.git',
    [string]$Platform = 'android',
    [string]$PackageKind = 'apk',
    [string]$Channel = 'stable',
    [switch]$Unlisted
)

$ErrorActionPreference = 'Stop'

$repoRoot = Split-Path -Parent $PSScriptRoot
$androidRoot = Join-Path $repoRoot 'src\Ikar.AndroidKotlin'
$prepareScript = Join-Path $androidRoot 'scripts\prepare-argus-publication.ps1'
$publishScript = Join-Path $androidRoot 'scripts\publish-release.ps1'

if (-not (Test-Path $prepareScript)) {
    throw "Argus preparation script was not found: $prepareScript"
}
if (-not (Test-Path $publishScript)) {
    throw "Argus SSH publication script was not found: $publishScript"
}

Push-Location $androidRoot
try {
    $prepareArgs = @(
        '-Slug', $Slug,
        '-Name', $AppName,
        '-Summary', $Summary,
        '-Description', $Description,
        '-Channel', $Channel,
        '-Platform', $Platform,
        '-Notes', $Notes,
        '-RepositoryUrl', $RepositoryUrl,
        '-HomepageUrl', $HomepageUrl
    )
    if ($Unlisted) {
        $prepareArgs += '-Unlisted'
    }

    & $prepareScript @prepareArgs
    if ($LASTEXITCODE -ne 0) {
        throw "Argus package preparation failed with exit code $LASTEXITCODE."
    }

    $metadataPath = Get-ChildItem -Path (Join-Path $androidRoot 'build\argus-publication') `
        -Recurse `
        -Filter 'argus-publication.json' |
        Sort-Object LastWriteTimeUtc -Descending |
        Select-Object -First 1
    if ($null -eq $metadataPath) {
        throw 'Prepared Argus publication metadata was not found.'
    }

    $metadata = Get-Content -LiteralPath $metadataPath.FullName -Raw | ConvertFrom-Json

    & $publishScript `
        -SshHost $ServerHost `
        -SshUsername $Username `
        -SshPassword $Password `
        -PublicBaseUrl $ArgusBaseUrl `
        -Slug $metadata.slug `
        -Name $metadata.name `
        -Summary $metadata.summary `
        -Description $metadata.description `
        -AndroidPackageName $metadata.androidPackageName `
        -Version $metadata.version `
        -AndroidVersionCode $metadata.androidVersionCode `
        -PackagePath $metadata.packagePath `
        -Channel $metadata.channel `
        -Platform $metadata.platform `
        -PackageKind $PackageKind `
        -Notes $metadata.notes `
        -RepositoryUrl $metadata.repositoryUrl `
        -HomepageUrl $metadata.homepageUrl `
        -Unlisted:([bool](-not $metadata.isListed))
} finally {
    Pop-Location
}
