#!/bin/bash
# milestone-create.sh - Create milestones on Gitea or GitHub
# Usage: milestone-create.sh -t "Title" [-d "Description"] [--due "YYYY-MM-DD"]

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/detect-platform.sh"

# Default values
TITLE=""
DESCRIPTION=""
DUE_DATE=""
LIST_ONLY=false

usage() {
    cat <<EOF
Usage: $(basename "$0") [OPTIONS]

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

Versioning Convention:
    - Features get dedicated milestones
    - Pre-release: 0.X.0 for breaking changes, 0.X.Y for patches
    - Post-release: X.0.0 for breaking changes
    - MVP starts at 0.1.0

Options:
    -t, --title TITLE       Milestone title/version (e.g., "0.2.0")
    -d, --desc DESCRIPTION  Milestone description
    --due DATE              Due date (YYYY-MM-DD format)
    --list                  List existing milestones
    -h, --help              Show this help message

Examples:
    $(basename "$0") --list
    $(basename "$0") -t "0.1.0" -d "MVP Release"
    $(basename "$0") -t "0.2.0" -d "User Authentication Feature" --due "2025-03-01"
EOF
    exit 1
}

# Parse arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        -t|--title)
            TITLE="$2"
            shift 2
            ;;
        -d|--desc)
            DESCRIPTION="$2"
            shift 2
            ;;
        --due)
            DUE_DATE="$2"
            shift 2
            ;;
        --list)
            LIST_ONLY=true
            shift
            ;;
        -h|--help)
            usage
            ;;
        *)
            echo "Unknown option: $1" >&2
            usage
            ;;
    esac
done

PLATFORM=$(detect_platform)

if [[ "$LIST_ONLY" == true ]]; then
    case "$PLATFORM" in
        github)
            gh api repos/:owner/:repo/milestones --jq '.[] | "\(.number)\t\(.title)\t\(.state)\t\(.open_issues)/\(.closed_issues) issues"'
            ;;
        gitea)
            tea milestones list
            ;;
        *)
            echo "Error: Could not detect git platform" >&2
            exit 1
            ;;
    esac
    exit 0
fi

if [[ -z "$TITLE" ]]; then
    echo "Error: Title is required (-t) for creating milestones" >&2
    usage
fi

case "$PLATFORM" in
    github)
        # GitHub uses the API for milestone creation
        JSON_PAYLOAD="{\"title\":\"$TITLE\""
        [[ -n "$DESCRIPTION" ]] && JSON_PAYLOAD="$JSON_PAYLOAD,\"description\":\"$DESCRIPTION\""
        [[ -n "$DUE_DATE" ]] && JSON_PAYLOAD="$JSON_PAYLOAD,\"due_on\":\"${DUE_DATE}T00:00:00Z\""
        JSON_PAYLOAD="$JSON_PAYLOAD}"

        gh api repos/:owner/:repo/milestones --method POST --input - <<< "$JSON_PAYLOAD"
        echo "Milestone '$TITLE' created successfully"
        ;;
    gitea)
        CMD="tea milestones create --title \"$TITLE\""
        [[ -n "$DESCRIPTION" ]] && CMD="$CMD --description \"$DESCRIPTION\""
        [[ -n "$DUE_DATE" ]] && CMD="$CMD --deadline \"$DUE_DATE\""
        eval "$CMD"
        echo "Milestone '$TITLE' created successfully"
        ;;
    *)
        echo "Error: Could not detect git platform" >&2
        exit 1
        ;;
esac
