#!/bin/bash
# Format Comment Script for Issue Sync
# Formats consolidated updates into a GitHub issue comment

set -euo pipefail

# Load libraries
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/../lib/logging-utils.sh"
source "${SCRIPT_DIR}/../lib/frontmatter-utils.sh"
source "${SCRIPT_DIR}/../lib/datetime-utils.sh"

# Script configuration
readonly ISSUE_NUMBER="${1:-}"
readonly UPDATES_FILE="${2:-}"
readonly PROGRESS_FILE="${3:-}"
readonly IS_COMPLETION="${4:-false}"

# Global variables
declare -g formatted_comment=""
declare -g temp_dir=""

# Main function
main() {
    print_banner "GitHub Comment Formatter" "1.0.0"

    # Validate inputs
    log_info "Formatting comment for issue #$ISSUE_NUMBER"
    validate_inputs || exit 1

    # Setup workspace
    setup_workspace

    # Format appropriate comment type
    if [[ "$IS_COMPLETION" == "true" ]]; then
        with_error_handling "Format completion comment" \
            format_completion_comment
    else
        with_error_handling "Format progress update comment" \
            format_progress_update_comment
    fi

    # Validate comment size
    with_error_handling "Validate comment size" \
        validate_comment_size

    # Output results
    display_results

    # Return path to formatted comment
    echo "$formatted_comment"
}

# Validate script inputs
validate_inputs() {
    log_function_entry "validate_inputs"

    if [[ -z "$ISSUE_NUMBER" ]]; then
        log_error "Issue number is required"
        return 1
    fi

    if [[ ! -f "$UPDATES_FILE" ]]; then
        log_error "Updates file not found: $UPDATES_FILE"
        return 1
    fi

    if [[ ! -f "$PROGRESS_FILE" ]]; then
        log_error "Progress file not found: $PROGRESS_FILE"
        return 1
    fi

    log_function_exit "validate_inputs"
    return 0
}

# Setup temporary workspace
setup_workspace() {
    log_function_entry "setup_workspace"

    temp_dir="/tmp/format-comment-$$"
    formatted_comment="$temp_dir/formatted-comment.md"

    log_info "Creating workspace: $temp_dir"
    mkdir -p "$temp_dir"

    # Cleanup on exit
    trap "cleanup_workspace" EXIT

    log_function_exit "setup_workspace"
}

# Cleanup workspace
cleanup_workspace() {
    if [[ -n "$temp_dir" ]] && [[ -d "$temp_dir" ]]; then
        log_debug "Cleaning up workspace: $temp_dir"
        # Don't remove - calling script needs the results
        # rm -rf "$temp_dir"
    fi
}

# Format progress update comment
format_progress_update_comment() {
    log_function_entry "format_progress_update_comment"

    local current_date
    current_date=$(get_current_date)

    local current_datetime
    current_datetime=$(get_current_datetime)

    # Get completion percentage
    local completion
    completion=$(get_frontmatter_field "$PROGRESS_FILE" "completion" 2>/dev/null || echo "0")

    # Parse updates file to extract sections
    parse_updates_file

    log_info "Creating progress update comment"

    # Create formatted comment
    cat > "$formatted_comment" << EOF
## 🔄 Progress Update - ${current_date}

### ✅ Completed Work
$(get_section "completed" "- No completed items in this update")

### 🔄 In Progress
$(get_section "in_progress" "- Continuing work on implementation")

### 📝 Technical Notes
$(get_section "technical_notes" "- No new technical notes")

### 📊 Acceptance Criteria Status
$(format_acceptance_criteria)

### 🚀 Next Steps
$(get_section "next_steps" "- Continue with planned implementation")

### ⚠️ Blockers
$(get_section "blockers" "- No current blockers")

### 💻 Recent Commits
$(format_recent_commits)

---
*Progress: ${completion}% | Synced from local updates at ${current_datetime}*
EOF

    log_success "Progress update comment formatted"
    log_function_exit "format_progress_update_comment"
}

# Format completion comment
format_completion_comment() {
    log_function_entry "format_completion_comment"

    local current_date
    current_date=$(get_current_date)

    local current_datetime
    current_datetime=$(get_current_datetime)

    # Parse updates file
    parse_updates_file

    log_info "Creating completion comment"

    # Create formatted completion comment
    cat > "$formatted_comment" << EOF
## ✅ Task Completed - ${current_date}

### 🎯 All Acceptance Criteria Met
$(format_acceptance_criteria_completion)

### 📦 Deliverables
$(get_section "deliverables" "- Implementation complete\n- Code reviewed and tested")

### 🧪 Testing
$(format_testing_status)

### 📚 Documentation
$(format_documentation_status)

This task is ready for review and can be closed.

---
*Task completed: 100% | Synced at ${current_datetime}*
EOF

    log_success "Completion comment formatted"
    log_function_exit "format_completion_comment"
}

# Parse updates file and extract sections
parse_updates_file() {
    log_function_entry "parse_updates_file"

    # Extract different sections from updates file
    extract_section "Progress Updates" "progress"
    extract_section "Technical Notes" "technical_notes"
    extract_section "Recent Commits" "commits"
    extract_section "Acceptance Criteria" "acceptance_criteria"
    extract_section "Next Steps" "next_steps"
    extract_section "Blockers" "blockers"
    extract_section "Completed Work" "completed"
    extract_section "In Progress" "in_progress"
    extract_section "Deliverables" "deliverables"
    extract_section "Testing" "testing"
    extract_section "Documentation" "documentation"

    log_debug "Updates file parsed into sections"
    log_function_exit "parse_updates_file"
}

# Extract a section from updates file
extract_section() {
    local section_title="$1"
    local section_key="$2"
    local section_file="$temp_dir/section_${section_key}.md"

    log_debug "Extracting section: $section_title"

    # Use awk to extract section content
    awk -v section="## $section_title" '
        $0 == section { capture=1; next }
        /^## / && capture { capture=0 }
        capture && NF { print }
    ' "$UPDATES_FILE" > "$section_file"

    if [[ ! -s "$section_file" ]]; then
        log_debug "Section not found or empty: $section_title"
    fi
}

# Get formatted section content
get_section() {
    local section_key="$1"
    local default_content="$2"
    local section_file="$temp_dir/section_${section_key}.md"

    if [[ -f "$section_file" ]] && [[ -s "$section_file" ]]; then
        cat "$section_file"
    else
        echo -e "$default_content"
    fi
}

# Format acceptance criteria
format_acceptance_criteria() {
    local criteria_file="$temp_dir/section_acceptance_criteria.md"

    if [[ -f "$criteria_file" ]] && [[ -s "$criteria_file" ]]; then
        # Process existing criteria
        cat "$criteria_file" | while read -r line; do
            if [[ "$line" =~ ^-[[:space:]].*$ ]]; then
                # Determine status based on keywords
                if [[ "$line" =~ (✅|[Cc]omplete|[Dd]one|[Ff]inished) ]]; then
                    echo "$line"
                elif [[ "$line" =~ (🔄|[Ii]n[[:space:]]progress|[Ww]orking|[Oo]ngoing) ]]; then
                    echo "$line"
                elif [[ "$line" =~ (⚠️|⏸️|[Bb]locked|[Pp]ending|[Ww]aiting) ]]; then
                    echo "$line"
                else
                    # Default to unchecked
                    echo "- □ ${line#- }"
                fi
            else
                echo "$line"
            fi
        done
    else
        # Default criteria status
        echo "- ✅ Initial implementation complete"
        echo "- 🔄 Testing in progress"
        echo "- □ Documentation pending"
    fi
}

# Format acceptance criteria for completion
format_acceptance_criteria_completion() {
    local criteria_file="$temp_dir/section_acceptance_criteria.md"

    if [[ -f "$criteria_file" ]] && [[ -s "$criteria_file" ]]; then
        # Mark all as complete
        cat "$criteria_file" | while read -r line; do
            if [[ "$line" =~ ^-[[:space:]].*$ ]]; then
                # Remove any existing status markers and add checkmark
                cleaned_line=$(echo "$line" | sed 's/^-[[:space:]]*\(✅\|🔄\|⚠️\|⏸️\|□\)[[:space:]]*/- /')
                echo "- ✅ ${cleaned_line#- }"
            else
                echo "$line"
            fi
        done
    else
        echo "- ✅ All requirements implemented"
        echo "- ✅ Code quality standards met"
        echo "- ✅ Performance requirements satisfied"
    fi
}

# Format recent commits
format_recent_commits() {
    local commits_file="$temp_dir/section_commits.md"

    if [[ -f "$commits_file" ]] && [[ -s "$commits_file" ]]; then
        # Limit to 10 most recent commits
        head -10 "$commits_file"
    else
        # Try to get recent commits from git
        if command -v git >/dev/null 2>&1; then
            local recent_commits
            recent_commits=$(git log --oneline -5 --no-merges 2>/dev/null || echo "")

            if [[ -n "$recent_commits" ]]; then
                echo "$recent_commits" | while read -r commit; do
                    echo "- $commit"
                done
            else
                echo "- No recent commits found"
            fi
        else
            echo "- Commit history not available"
        fi
    fi
}

# Format testing status
format_testing_status() {
    local testing_file="$temp_dir/section_testing.md"

    if [[ -f "$testing_file" ]] && [[ -s "$testing_file" ]]; then
        cat "$testing_file"
    else
        # Default testing status
        cat << EOF
- Unit tests: ✅ Passing
- Integration tests: ✅ Passing
- Manual testing: ✅ Complete
EOF
    fi
}

# Format documentation status
format_documentation_status() {
    local docs_file="$temp_dir/section_documentation.md"

    if [[ -f "$docs_file" ]] && [[ -s "$docs_file" ]]; then
        cat "$docs_file"
    else
        # Default documentation status
        cat << EOF
- Code documentation: ✅ Updated
- README updates: ✅ Complete
- API documentation: ✅ Current
EOF
    fi
}

# Validate comment size against GitHub limits
validate_comment_size() {
    log_function_entry "validate_comment_size"

    local max_size=65536  # GitHub comment size limit
    local comment_size
    comment_size=$(wc -c < "$formatted_comment")

    log_info "Comment size: $comment_size characters (max: $max_size)"

    if [[ "$comment_size" -gt "$max_size" ]]; then
        log_warning "Comment exceeds GitHub size limit ($comment_size > $max_size)"

        # Truncate comment
        local truncated_file="$temp_dir/truncated-comment.md"
        head -c 65000 "$formatted_comment" > "$truncated_file"

        # Add truncation notice
        cat >> "$truncated_file" << EOF

---
⚠️ **Comment truncated due to size limit**

The full update is available in your local files at:
\`.opencode/epics/*/updates/$ISSUE_NUMBER/\`
EOF

        # Replace with truncated version
        mv "$truncated_file" "$formatted_comment"

        log_info "Comment truncated to fit GitHub limits"
    else
        log_debug "Comment size is within limits"
    fi

    log_function_exit "validate_comment_size"
}

# Display formatting results
display_results() {
    print_section "✅ Comment Formatting Results"

    echo "Issue: #$ISSUE_NUMBER"
    echo "Comment Type: $([[ "$IS_COMPLETION" == "true" ]] && echo "Completion" || echo "Progress Update")"

    # Show comment preview (first 5 lines)
    echo ""
    echo "Comment Preview:"
    head -5 "$formatted_comment" | sed 's/^/  /'
    echo "  ..."

    # Show size info
    local comment_size
    comment_size=$(wc -c < "$formatted_comment")
    echo ""
    echo "Comment Size: $comment_size characters"

    if [[ "$comment_size" -gt 65536 ]]; then
        echo "⚠️ Comment was truncated to fit GitHub limits"
    fi

    echo ""
    echo "✅ Comment formatted successfully"
    echo "📄 Formatted comment: $formatted_comment"
}

# Error handling
handle_error() {
    local exit_code=$?
    log_error "Comment formatting failed with exit code: $exit_code"
    log_error "Failed to format comment for issue: #$ISSUE_NUMBER"
    exit "$exit_code"
}

# Set up error handling
trap handle_error ERR

# Validate arguments
if [[ $# -lt 3 ]]; then
    echo "Usage: $0 <issue_number> <updates_file> <progress_file> [is_completion]"
    echo ""
    echo "Formats consolidated updates into a GitHub issue comment."
    echo ""
    echo "Arguments:"
    echo "  issue_number    GitHub issue number"
    echo "  updates_file    Path to consolidated updates file"
    echo "  progress_file   Path to progress.md file"
    echo "  is_completion   Optional: 'true' if task is complete (default: false)"
    echo ""
    echo "The script will format either:"
    echo "  - Progress update comment (default)"
    echo "  - Completion comment (if is_completion=true)"
    echo ""
    echo "GitHub comment size limits:"
    echo "  - Maximum: 65,536 characters"
    echo "  - Comments will be truncated if necessary"
    echo ""
    echo "Examples:"
    echo "  $0 123 /tmp/updates.md .opencode/epics/auth/updates/123/progress.md"
    echo "  $0 456 /tmp/updates.md ./progress.md true"
    echo ""
    echo "Output:"
    echo "  Prints path to formatted comment file on stdout"
    echo ""
    exit 1
fi

# Run main function
main "$@"