#!/bin/bash
# init-repo-labels.sh - Create standard labels and initial milestone for a repository
# Usage: init-repo-labels.sh [--skip-milestone]
#
# Works with both Gitea (tea) and GitHub (gh).

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
GIT_SCRIPT_DIR="$HOME/.config/mosaic/tools/git"
source "$GIT_SCRIPT_DIR/detect-platform.sh"

SKIP_MILESTONE=false

while [[ $# -gt 0 ]]; do
    case $1 in
        --skip-milestone)
            SKIP_MILESTONE=true
            shift
            ;;
        -h|--help)
            echo "Usage: $(basename "$0") [--skip-milestone]"
            echo ""
            echo "Create standard labels and initial milestone for the current repository."
            echo ""
            echo "Options:"
            echo "  --skip-milestone   Skip creating the 0.0.1 pre-MVP milestone"
            echo "  -h, --help         Show this help"
            exit 0
            ;;
        *)
            echo "Unknown option: $1" >&2
            exit 1
            ;;
    esac
done

PLATFORM=$(detect_platform)
OWNER=$(get_repo_owner)
REPO=$(get_repo_name)

echo "Platform: $PLATFORM"
echo "Repository: $OWNER/$REPO"
echo ""

# Standard labels with colors
# Format: "name|color|description"
LABELS=(
    "epic|3E4B9E|Large feature spanning multiple issues"
    "feature|0E8A16|New functionality"
    "bug|D73A4A|Defect fix"
    "task|0075CA|General work item"
    "documentation|0075CA|Documentation updates"
    "security|B60205|Security-related"
    "breaking|D93F0B|Breaking change"
)

create_label_github() {
    local name="$1" color="$2" description="$3"

    # Check if label already exists
    if gh label list --repo "$OWNER/$REPO" --json name -q ".[].name" 2>/dev/null | grep -qx "$name"; then
        echo "  [skip] '$name' already exists"
        return 0
    fi

    gh label create "$name" \
        --repo "$OWNER/$REPO" \
        --color "$color" \
        --description "$description" 2>/dev/null && \
        echo "  [created] '$name'" || \
        echo "  [error] Failed to create '$name'"
}

create_label_gitea() {
    local name="$1" color="$2" description="$3"

    # Check if label already exists
    if tea labels list 2>/dev/null | grep -q "$name"; then
        echo "  [skip] '$name' already exists"
        return 0
    fi

    tea labels create --name "$name" --color "#$color" --description "$description" 2>/dev/null && \
        echo "  [created] '$name'" || \
        echo "  [error] Failed to create '$name'"
}

echo "Creating labels..."

for label_def in "${LABELS[@]}"; do
    IFS='|' read -r name color description <<< "$label_def"

    case "$PLATFORM" in
        github)
            create_label_github "$name" "$color" "$description"
            ;;
        gitea)
            create_label_gitea "$name" "$color" "$description"
            ;;
        *)
            echo "Error: Unsupported platform '$PLATFORM'" >&2
            exit 1
            ;;
    esac
done

echo ""

# Create initial pre-MVP milestone
if [[ "$SKIP_MILESTONE" != true ]]; then
    echo "Creating initial pre-MVP milestone..."

    "$GIT_SCRIPT_DIR/milestone-create.sh" -t "0.0.1" -d "Pre-MVP - Foundation Sprint" 2>/dev/null && \
        echo "  [created] Milestone '0.0.1 - Pre-MVP'" || \
        echo "  [skip] Milestone may already exist or creation failed"

    echo "  [note] Reserve 0.1.0 for MVP release milestone"

    echo ""
fi

echo "Label initialization complete."
