bin/octa.ps1

#Requires -Version 5.1
<#
    .SYNOPSIS
    Octa CLI entry point. See specs/001-opt-in-debloat-categories/contracts/cli-contract.md.

    .EXAMPLE
    octa --list
    octa --telemetry
    octa --telemetry --appx --clean
    octa --quick --clean --json
    octa --undo latest
#>


Import-Module (Join-Path $PSScriptRoot '..\Octa.psd1') -Force

$knownCategoryIds = @(Get-OctaCategory | Select-Object -ExpandProperty Id)

$apply = $false
$quick = $false
$noRestorePoint = $false
$yes = $false
$asJson = $false
$quiet = $false
$list = $false
$undoTarget = $null
$categoryIds = @()
$dashboard = $false
$startupManager = $false
$startupDisable = $null
$tasksBrowser = $false
$taskEnable = $null
$taskDisable = $null
$taskRun = $null
$taskStop = $null
$taskDelete = $null
$diskCleanup = $false

$i = 0
while ($i -lt $args.Count) {
    $arg = $args[$i]
    # NOTE: PowerShell's switch evaluates every matching case, not just the first (no C-style
    # implicit fallthrough prevention) - each case needs an explicit `break`, otherwise a flag
    # like --list also falls into the generic --<category> case below and errors out.
    switch -Regex ($arg) {
        '^--clean$' { $apply = $true; break }
        '^--quick$' { $quick = $true; break }
        '^--no-restore-point$' { $noRestorePoint = $true; break }
        '^--yes$' { $yes = $true; break }
        '^--json$' { $asJson = $true; break }
        '^(--quiet|-q)$' { $quiet = $true; break }
        '^--verbose$' { break } # activity panel is verbose by nature - no separate flag behavior needed yet
        '^--list$' { $list = $true; break }
        '^--undo$' { $i++; $undoTarget = $args[$i]; break }
        '^--dashboard$' { $dashboard = $true; break }
        '^--startup-manager$' { $startupManager = $true; break }
        '^--disk-cleanup$' { $diskCleanup = $true; break }
        '^--tasks$' { $tasksBrowser = $true; break }
        '^--disable$' {
            $i++
            if ($tasksBrowser) { $taskDisable = $args[$i] } else { $startupDisable = $args[$i] }
            break
        }
        '^--enable$' { $i++; $taskEnable = $args[$i]; break }
        '^--run$' { $i++; $taskRun = $args[$i]; break }
        '^--stop$' { $i++; $taskStop = $args[$i]; break }
        '^--delete$' { $i++; $taskDelete = $args[$i]; break }
        '^--(.+)$' {
            $candidate = $Matches[1]
            if ($candidate -in $knownCategoryIds) {
                $categoryIds += $candidate
            }
            else {
                Write-Error "Unknown flag or category: --$candidate"
                exit 5
            }
            break
        }
    }
    $i++
}

if ($list) {
    Get-OctaCategory | Select-Object Id, DisplayName, RequiresElevation, ContainsIrreversibleActions, HighestRiskLevel |
        Format-Table -AutoSize
    exit 0
}

if ($undoTarget) {
    $result = Undo-OctaRun -RunId $undoTarget
    if ($asJson) { $result | ConvertTo-Json -Depth 6 } else { $result | Format-List }
    if ($result.Status -eq 'NotFound') { exit 4 }
    exit 0
}

if ($dashboard) {
    Show-OctaDashboard | Out-Null
    exit 0
}

if ($startupManager) {
    if ($startupDisable) {
        $result = Invoke-OctaStartupManager -Disable $startupDisable
        if ($asJson) { $result | ConvertTo-Json -Depth 6 } else { $result | Format-List }
    }
    else {
        Invoke-OctaStartupManager | Out-Null
    }
    exit 0
}

if ($tasksBrowser) {
    if ($taskEnable) { $result = Invoke-OctaTaskBrowser -Enable $taskEnable }
    elseif ($taskDisable) { $result = Invoke-OctaTaskBrowser -Disable $taskDisable }
    elseif ($taskRun) { $result = Invoke-OctaTaskBrowser -Run $taskRun }
    elseif ($taskStop) { $result = Invoke-OctaTaskBrowser -Stop $taskStop }
    elseif ($taskDelete) { $result = Invoke-OctaTaskBrowser -Delete $taskDelete -Yes:$yes }
    else { Invoke-OctaTaskBrowser | Out-Null; exit 0 }

    if ($asJson) { $result | ConvertTo-Json -Depth 6 } else { $result | Format-List }
    exit 0
}

if ($diskCleanup) {
    $result = Invoke-OctaDiskCleanup -Apply:$apply -Yes:$yes
    if ($asJson) { $result | ConvertTo-Json -Depth 6 } else { $result | Format-List }
    exit 0
}

if (-not $quick -and $categoryIds.Count -eq 0) {
    Invoke-Octa
    exit 0
}

if ($quick) {
    $result = Invoke-OctaQuickClean -Apply:$apply -NoRestorePoint:$noRestorePoint -Yes:$yes -Quiet:$quiet
}
else {
    $result = Invoke-OctaCategory -CategoryId $categoryIds -Apply:$apply -NoRestorePoint:$noRestorePoint -Yes:$yes -Quiet:$quiet
}

if ($asJson) { $result | ConvertTo-Json -Depth 6 } else { $result | Format-List }

switch ($result.Status) {
    'Cancelled' { exit 1 }
    'ElevationRequired' { exit 3 }
    'UnknownCategory' { exit 5 }
    'Error' { exit 5 }
    default { exit 0 }
}