# issue-assign.ps1 - Assign issues on Gitea or GitHub
# Usage: .\issue-assign.ps1 -Issue ISSUE_NUMBER [-Assignee assignee] [-Labels labels] [-Milestone milestone]

[CmdletBinding()]
param(
    [Parameter(Mandatory=$true)]
    [Alias("i")]
    [int]$Issue,

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

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

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

    [Alias("r")]
    [switch]$RemoveAssignee,

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

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

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

Assign or update an issue on the current repository (Gitea or GitHub).

Options:
    -Issue, -i NUMBER       Issue number (required)
    -Assignee, -a USER      Assign to user (use @me for self)
    -Labels, -l LABELS      Add comma-separated labels
    -Milestone, -m NAME     Set milestone
    -RemoveAssignee, -r     Remove current assignee
    -Help, -h               Show this help message

Examples:
    .\issue-assign.ps1 -i 42 -a "username"
    .\issue-assign.ps1 -i 42 -l "in-progress" -m "0.2.0"
    .\issue-assign.ps1 -i 42 -a @me
"@
    exit 1
}

if ($Help) {
    Show-Usage
}

$platform = Get-GitPlatform

switch ($platform) {
    "github" {
        if ($Assignee) {
            gh issue edit $Issue --add-assignee $Assignee
        }
        if ($RemoveAssignee) {
            $current = gh issue view $Issue --json assignees -q '.assignees[].login' 2>$null
            if ($current) {
                $assignees = ($current -split "`n") -join ","
                gh issue edit $Issue --remove-assignee $assignees
            }
        }
        if ($Labels) {
            gh issue edit $Issue --add-label $Labels
        }
        if ($Milestone) {
            gh issue edit $Issue --milestone $Milestone
        }
        Write-Host "Issue #$Issue updated successfully"
    }
    "gitea" {
        $repoArgs = @(Get-GiteaRepoArgs)
        if ($repoArgs.Length -eq 0) {
            Write-Error "Could not resolve Gitea repo/login for remote host"
            exit 1
        }
        $needsEdit = $false
        $cmd = @("tea", "issue", "edit", $Issue)

        if ($Assignee) {
            $cmd += @("--assignees", $Assignee)
            $needsEdit = $true
        }
        if ($Labels) {
            $cmd += @("--labels", $Labels)
            $needsEdit = $true
        }
        if ($Milestone) {
            $milestoneList = tea milestones list @repoArgs 2>$null
            $milestoneId = ($milestoneList | Select-String "^\s*(\d+).*$Milestone" | ForEach-Object { $_.Matches.Groups[1].Value } | Select-Object -First 1)
            if ($milestoneId) {
                $cmd += @("--milestone", $milestoneId)
                $needsEdit = $true
            } else {
                Write-Warning "Could not find milestone '$Milestone'"
            }
        }

        if ($needsEdit) {
            $cmd += $repoArgs
            & $cmd[0] $cmd[1..($cmd.Length-1)]
            Write-Host "Issue #$Issue updated successfully"
        } else {
            Write-Host "No changes specified"
        }
    }
    default {
        Write-Error "Could not detect git platform"
        exit 1
    }
}
