# milestone-create.ps1 - Create milestones on Gitea or GitHub
# Usage: .\milestone-create.ps1 -Title "Title" [-Description "Description"] [-Due "YYYY-MM-DD"]

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

    [Alias("d")]
    [string]$Description,

    [string]$Due,

    [switch]$List,

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

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

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

Create or list milestones on the current repository (Gitea or GitHub).

Versioning Convention:
    - Features get dedicated milestones
    - Pre-MVP milestones MUST use 0.0.x and MUST start at 0.0.1
    - 0.1.0 is reserved for MVP release
    - After MVP, continue semantic progression (0.1.x, 0.2.x, ...)

Options:
    -Title, -t TITLE        Milestone title/version (e.g., "0.0.1")
    -Description, -d DESC   Milestone description
    -Due DATE               Due date (YYYY-MM-DD format)
    -List                   List existing milestones
    -Help, -h               Show this help message

Examples:
    .\milestone-create.ps1 -List
    .\milestone-create.ps1 -t "0.0.1" -d "Pre-MVP Foundation Sprint"
    .\milestone-create.ps1 -t "0.1.0" -d "MVP Release" -Due "2025-03-01"
"@
    exit 1
}

if ($Help) {
    Show-Usage
}

$platform = Get-GitPlatform

if ($List) {
    switch ($platform) {
        "github" {
            gh api repos/:owner/:repo/milestones --jq '.[] | "\(.number)`t\(.title)`t\(.state)`t\(.open_issues)/\(.closed_issues) issues"'
        }
        "gitea" {
            $repoArgs = @(Get-GiteaRepoArgs)
            if ($repoArgs.Length -eq 0) {
                Write-Error "Could not resolve Gitea repo/login for remote host"
                exit 1
            }
            tea milestones list @repoArgs
        }
        default {
            Write-Error "Could not detect git platform"
            exit 1
        }
    }
    exit 0
}

if (-not $Title) {
    Write-Error "Title is required (-t) for creating milestones"
    Show-Usage
}

switch ($platform) {
    "github" {
        $payload = @{ title = $Title }
        if ($Description) { $payload.description = $Description }
        if ($Due) { $payload.due_on = "${Due}T00:00:00Z" }

        $json = $payload | ConvertTo-Json -Compress
        $json | gh api repos/:owner/:repo/milestones --method POST --input -
        Write-Host "Milestone '$Title' created successfully"
    }
    "gitea" {
        $repoArgs = @(Get-GiteaRepoArgs)
        if ($repoArgs.Length -eq 0) {
            Write-Error "Could not resolve Gitea repo/login for remote host"
            exit 1
        }
        $cmd = @("tea", "milestones", "create", "--title", $Title)
        if ($Description) { $cmd += @("--description", $Description) }
        if ($Due) { $cmd += @("--deadline", $Due) }
        $cmd += $repoArgs
        & $cmd[0] $cmd[1..($cmd.Length-1)]
        Write-Host "Milestone '$Title' created successfully"
    }
    default {
        Write-Error "Could not detect git platform"
        exit 1
    }
}
