#!/bin/bash

# Product Owner Assistant - Main Script
# Implements epic and user story management for Gitea repositories

set -e

# Configuration
SKILL_DIR="/Users/gabrielradureau/Work/Vibe/DanceLessonsCoach/.vibe/skills/product-owner-assistant"
DATA_DIR="$SKILL_DIR/data"
GITEA_CLIENT="skill gitea-client"

# Ensure data directory exists
mkdir -p "$DATA_DIR"

# Default labels
EPIC_LABELS="epic,backlog"
STORY_LABELS="story,backlog"

# Usage function
usage() {
    echo "Usage: $0 <command> [args...]"
    echo ""
    echo "Commands:"
    echo "  create-epic <owner> <repo> <title> <description> [labels]"
    echo "  create-story <owner> <repo> <epic_id> <title> <description> [labels]"
    echo "  link-to-epic <owner> <repo> <issue_number> <epic_id>"
    echo "  epic-progress <owner> <repo> <epic_id>"
    echo "  list-epics <owner> <repo> [state]"
}

# Main command router
main() {
    local command="$1"
    shift
    
    case "$command" in
        create-epic)
            create_epic "$@"
            ;;
        create-story)
            create_story "$@"
            ;;
        link-to-epic)
            link_to_epic "$@"
            ;;
        epic-progress)
            epic_progress "$@"
            ;;
        list-epics)
            list_epics "$@"
            ;;
        *)
            echo "Unknown command: $command"
            usage
            exit 1
            ;;
    esac
}

# Create an epic
create_epic() {
    local owner="$1"
    local repo="$2"
    local title="$3"
    local description="$4"
    local labels="${5:-$EPIC_LABELS}"
    
    echo "Creating epic: $title"
    
    # Create the issue
    $GITEA_CLIENT create-issue "$owner" "$repo" "$title" "$description" "$labels"
    
    # Get the issue number
    local issue_number=$($GITEA_CLIENT list-issues "$owner" "$repo" | grep "$title" | head -1 | awk '{print $1}')
    
    # Store epic metadata
    echo "$issue_number" > "$DATA_DIR/epic_$issue_number.meta"
    echo "title=$title" >> "$DATA_DIR/epic_$issue_number.meta"
    echo "created=$(date +%Y-%m-%d)" >> "$DATA_DIR/epic_$issue_number.meta"
    
    echo "Epic created: #$issue_number"
    echo "$issue_number"
}

# Create a user story
create_story() {
    local owner="$1"
    local repo="$2"
    local epic_id="$3"
    local title="$4"
    local description="$5"
    local labels="${6:-$STORY_LABELS}"
    
    echo "Creating story under epic #$epic_id: $title"
    
    # Create the story issue
    $GITEA_CLIENT create-issue "$owner" "$repo" "$title" "$description" "$labels"
    
    # Get the story issue number
    local story_number=$($GITEA_CLIENT list-issues "$owner" "$repo" | grep "$title" | head -1 | awk '{print $1}')
    
    # Link story to epic by commenting on the epic
    $GITEA_CLIENT comment-issue "$owner" "$repo" "$epic_id" "Linked story: #$story_number - $title"
    
    # Also comment on the story to reference the epic
    $GITEA_CLIENT comment-issue "$owner" "$repo" "$story_number" "Part of epic: #$epic_id"
    
    # Store story metadata
    echo "$story_number" > "$DATA_DIR/story_$story_number.meta"
    echo "epic=$epic_id" >> "$DATA_DIR/story_$story_number.meta"
    echo "title=$title" >> "$DATA_DIR/story_$story_number.meta"
    echo "created=$(date +%Y-%m-%d)" >> "$DATA_DIR/story_$story_number.meta"
    
    echo "Story created: #$story_number"
    echo "$story_number"
}

# Link existing issue to epic
link_to_epic() {
    local owner="$1"
    local repo="$2"
    local issue_number="$3"
    local epic_id="$4"
    
    echo "Linking issue #$issue_number to epic #$epic_id"
    
    # Get issue title
    local issue_title=$($GITEA_CLIENT show-issue "$owner" "$repo" "$issue_number" | jq -r '.title')
    
    # Comment on epic
    $GITEA_CLIENT comment-issue "$owner" "$repo" "$epic_id" "Linked issue: #$issue_number - $issue_title"
    
    # Comment on issue
    $GITEA_CLIENT comment-issue "$owner" "$repo" "$issue_number" "Part of epic: #$epic_id"
    
    # Store relationship
    echo "$epic_id" > "$DATA_DIR/issue_$issue_number.epic"
    
    echo "Issue #$issue_number linked to epic #$epic_id"
}

# Show epic progress
epic_progress() {
    local owner="$1"
    local repo="$2"
    local epic_id="$3"
    
    echo "=== Epic Progress: #$epic_id ==="
    
    # Get epic details
    local epic_data=$($GITEA_CLIENT show-issue "$owner" "$repo" "$epic_id")
    local epic_title=$(echo "$epic_data" | jq -r '.title')
    local epic_state=$(echo "$epic_data" | jq -r '.state')
    
    echo "Title: $epic_title"
    echo "State: $epic_state"
    echo ""
    
    # Find linked stories
    echo "Linked Stories:"
    local comments=$($GITEA_CLIENT show-issue "$owner" "$repo" "$epic_id" | jq -r '.comments[].body')
    
    local story_count=0
    
    while IFS= read -r comment; do
        if [[ $comment == *"Linked story: #"* ]]; then
            local story_number=$(echo "$comment" | grep -oP '#\K\d+')
            local story_title=$(echo "$comment" | sed 's/.*#'