# issue-list.ps1 - List issues on Gitea or GitHub
# Usage: .\issue-list.ps1 [-State state] [-Label label] [-Milestone milestone] [-Assignee assignee]

[CmdletBinding()]
param(
    [Alias("s")]
    [ValidateSet("open", "closed", "all")]
    [string]$State = "open",

    [Alias("l")]
    [string]$Label,

    [Alias("m")]
    [string]$Milestone,

    [Alias("a")]
    [string]$Assignee,

    [Alias("n")]
    [int]$Limit = 100,

    [Alias("h")]
    [switch]$Help
)

$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$ScriptDir\detect-platform.ps1"

function Show-Usage {
    @"
Usage: issue-list.ps1 [OPTIONS]

List issues from the current repository (Gitea or GitHub).

Options:
    -State, -s STATE        Filter by state: open, closed, all (default: open)
    -Label, -l LABEL        Filter by label
    -Milestone, -m NAME     Filter by milestone name
    -Assignee, -a USER      Filter by assignee
    -Limit, -n N            Maximum issues to show (default: 100)
    -Help, -h               Show this help message

Examples:
    .\issue-list.ps1                              # List open issues
    .\issue-list.ps1 -s all -l bug                # All issues with 'bug' label
    .\issue-list.ps1 -m "0.2.0"                   # Issues in milestone 0.2.0
"@
    exit 1
}

if ($Help) {
    Show-Usage
}

$platform = Get-GitPlatform

switch ($platform) {
    "github" {
        $cmd = @("gh", "issue", "list", "--state", $State, "--limit", $Limit)
        if ($Label) { $cmd += @("--label", $Label) }
        if ($Milestone) { $cmd += @("--milestone", $Milestone) }
        if ($Assignee) { $cmd += @("--assignee", $Assignee) }
        & $cmd[0] $cmd[1..($cmd.Length-1)]
    }
    "gitea" {
        $repoArgs = @(Get-GiteaRepoArgs)
        if ($repoArgs.Length -eq 0) {
            Write-Error "Could not resolve Gitea repo/login for remote host"
            exit 1
        }
        $cmd = @("tea", "issues", "list", "--state", $State, "--limit", $Limit)
        if ($Label) { $cmd += @("--labels", $Label) }
        if ($Milestone) { $cmd += @("--milestones", $Milestone) }
        $cmd += $repoArgs
        & $cmd[0] $cmd[1..($cmd.Length-1)]
        if ($Assignee) {
            Write-Warning "Assignee filtering may require manual review for Gitea"
        }
    }
    default {
        Write-Error "Could not detect git platform"
        exit 1
    }
}
