#!/bin/bash
# {{ name }} - Check Prerequisites Script
# Generated by MetaSpec

set -e

# Configuration
SPECS_DIR="{{ specs_dir | default('specs') }}"
CLI_PREFIX="{{ cli_prefix | default('metaspec') }}"

# Parse options
JSON_OUTPUT=false
PATHS_ONLY=false
REQUIRE_TASKS=false
INCLUDE_TASKS=false

while [[ $# -gt 0 ]]; do
    case $1 in
        --json)
            JSON_OUTPUT=true
            shift
            ;;
        --paths-only)
            PATHS_ONLY=true
            shift
            ;;
        --require-tasks)
            REQUIRE_TASKS=true
            shift
            ;;
        --include-tasks)
            INCLUDE_TASKS=true
            shift
            ;;
        *)
            echo "Unknown option: $1" >&2
            exit 1
            ;;
    esac
done

# Detect current feature directory
# Priority: SPECIFY_FEATURE env var > Git branch > error
if [ -n "$SPECIFY_FEATURE" ]; then
    FEATURE_DIR="${SPECS_DIR}/${SPECIFY_FEATURE}"
elif command -v git &> /dev/null && git rev-parse --git-dir > /dev/null 2>&1; then
    # Try to detect from Git branch
    BRANCH=$(git branch --show-current 2>/dev/null)
    if [[ "$BRANCH" =~ ^[0-9]+-.*$ ]]; then
        FEATURE_DIR="${SPECS_DIR}/${BRANCH}"
    else
        echo "Error: Not on a feature branch. Set SPECIFY_FEATURE environment variable." >&2
        echo "Example: export SPECIFY_FEATURE=001-my-feature" >&2
        exit 1
    fi
else
    echo "Error: Cannot detect feature. Set SPECIFY_FEATURE environment variable." >&2
    echo "Example: export SPECIFY_FEATURE=001-my-feature" >&2
    exit 1
fi

# Check if feature directory exists
if [ ! -d "$FEATURE_DIR" ]; then
    echo "Error: Feature directory not found: $FEATURE_DIR" >&2
    exit 1
fi

# Check required files
SPEC_FILE="${FEATURE_DIR}/spec.md"
PLAN_FILE="${FEATURE_DIR}/plan.md"
TASKS_FILE="${FEATURE_DIR}/tasks.md"

# Validate spec.md exists
if [ ! -f "$SPEC_FILE" ]; then
    echo "Error: Specification not found: $SPEC_FILE" >&2
    echo "Run: /${CLI_PREFIX}.specify" >&2
    exit 1
fi

# For paths-only mode, we only need spec.md
if [ "$PATHS_ONLY" = true ]; then
    if [ "$JSON_OUTPUT" = true ]; then
        echo "{"
        echo "  \"FEATURE_DIR\": \"$(realpath "$FEATURE_DIR")\","
        echo "  \"FEATURE_SPEC\": \"$(realpath "$SPEC_FILE")\""
        echo "}"
    else
        echo "FEATURE_DIR=$(realpath "$FEATURE_DIR")"
        echo "FEATURE_SPEC=$(realpath "$SPEC_FILE")"
    fi
    exit 0
fi

# Check plan.md for other modes
AVAILABLE_DOCS=("spec.md")
if [ -f "$PLAN_FILE" ]; then
    AVAILABLE_DOCS+=("plan.md")
else
    echo "Warning: Plan not found: $PLAN_FILE" >&2
    echo "Run: /${CLI_PREFIX}.plan" >&2
fi

# Check tasks.md if required
if [ "$REQUIRE_TASKS" = true ]; then
    if [ ! -f "$TASKS_FILE" ]; then
        echo "Error: Tasks not found: $TASKS_FILE" >&2
        echo "Run: /${CLI_PREFIX}.tasks" >&2
        exit 1
    fi
    AVAILABLE_DOCS+=("tasks.md")
elif [ "$INCLUDE_TASKS" = true ] && [ -f "$TASKS_FILE" ]; then
    AVAILABLE_DOCS+=("tasks.md")
fi

# Output results
if [ "$JSON_OUTPUT" = true ]; then
    echo "{"
    echo "  \"FEATURE_DIR\": \"$(realpath "$FEATURE_DIR")\","
    echo "  \"FEATURE_SPEC\": \"$(realpath "$SPEC_FILE")\","
    
    if [ -f "$PLAN_FILE" ]; then
        echo "  \"IMPL_PLAN\": \"$(realpath "$PLAN_FILE")\","
    fi
    
    if [ -f "$TASKS_FILE" ]; then
        echo "  \"TASKS\": \"$(realpath "$TASKS_FILE")\","
    fi
    
    # Available docs array
    echo "  \"AVAILABLE_DOCS\": ["
    for i in "${!AVAILABLE_DOCS[@]}"; do
        if [ $i -eq $((${#AVAILABLE_DOCS[@]} - 1)) ]; then
            echo "    \"${AVAILABLE_DOCS[$i]}\""
        else
            echo "    \"${AVAILABLE_DOCS[$i]}\","
        fi
    done
    echo "  ]"
    echo "}"
else
    echo "FEATURE_DIR=$(realpath "$FEATURE_DIR")"
    echo "FEATURE_SPEC=$(realpath "$SPEC_FILE")"
    [ -f "$PLAN_FILE" ] && echo "IMPL_PLAN=$(realpath "$PLAN_FILE")"
    [ -f "$TASKS_FILE" ] && echo "TASKS=$(realpath "$TASKS_FILE")"
    echo "AVAILABLE_DOCS=${AVAILABLE_DOCS[*]}"
fi

