# pr-create.ps1 - Create pull requests on Gitea or GitHub
# Usage: .\pr-create.ps1 -Title "Title" [-Body "Body"] [-Base base] [-Head head] [-Labels "labels"] [-Milestone "milestone"]

[CmdletBinding()]
param(
    [Alias("t")]
    [string]$Title,

    [Alias("b")]
    [string]$Body,

    [string]$Base,

    [Alias("H")]
    [string]$Head,

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

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

    [Alias("i")]
    [int]$Issue,

    [Alias("d")]
    [switch]$Draft,

    [switch]$Help
)

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

function Show-Usage {
    @"
Usage: pr-create.ps1 [OPTIONS]

Create a pull request on the current repository (Gitea or GitHub).

Options:
    -Title, -t TITLE        PR title (required, or use -Issue)
    -Body, -b BODY          PR description/body
    -Base, -B BRANCH        Base branch to merge into (default: main/master)
    -Head, -H BRANCH        Head branch with changes (default: current branch)
    -Labels, -l LABELS      Comma-separated labels
    -Milestone, -m NAME     Milestone name
    -Issue, -i NUMBER       Link to issue (auto-generates title if not provided)
    -Draft, -d              Create as draft PR
    -Help                   Show this help message

Examples:
    .\pr-create.ps1 -Title "Add login feature" -Body "Implements user authentication"
    .\pr-create.ps1 -t "Fix bug" -B main -H feature/fix-123
    .\pr-create.ps1 -i 42 -b "Implements the feature described in #42"
    .\pr-create.ps1 -t "WIP: New feature" -Draft
"@
    exit 1
}

if ($Help) {
    Show-Usage
}

# If no title but issue provided, generate title
if (-not $Title -and $Issue) {
    $Title = "Fixes #$Issue"
}

if (-not $Title) {
    Write-Error "Title is required (-t) or provide an issue (-i)"
    Show-Usage
}

# Default head branch to current branch
if (-not $Head) {
    $Head = git branch --show-current
}

# Add issue reference to body if provided
if ($Issue) {
    if ($Body) {
        $Body = "$Body`n`nFixes #$Issue"
    } else {
        $Body = "Fixes #$Issue"
    }
}

$platform = Get-GitPlatform

switch ($platform) {
    "github" {
        $cmd = @("gh", "pr", "create", "--title", $Title)
        if ($Body) { $cmd += @("--body", $Body) }
        if ($Base) { $cmd += @("--base", $Base) }
        if ($Head) { $cmd += @("--head", $Head) }
        if ($Labels) { $cmd += @("--label", $Labels) }
        if ($Milestone) { $cmd += @("--milestone", $Milestone) }
        if ($Draft) { $cmd += "--draft" }
        & $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", "pr", "create", "--title", $Title)
        if ($Body) { $cmd += @("--description", $Body) }
        if ($Base) { $cmd += @("--base", $Base) }
        if ($Head) { $cmd += @("--head", $Head) }
        if ($Labels) { $cmd += @("--labels", $Labels) }

        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)
            } else {
                Write-Warning "Could not find milestone '$Milestone', creating without milestone"
            }
        }

        if ($Draft) {
            Write-Warning "Draft PR may not be supported by your tea version"
        }

        $cmd += $repoArgs
        & $cmd[0] $cmd[1..($cmd.Length-1)]
    }
    default {
        Write-Error "Could not detect git platform"
        exit 1
    }
}
