#!/bin/bash
#
# Refresh the Hyva UI component catalog by scanning the installed hyva-ui package
#
# Usage: refresh_catalog.sh [hyva_ui_path] [output_file]
#   hyva_ui_path: Path to hyva-ui directory (default: vendor/hyva-themes/hyva-ui)
#   output_file: Output markdown file (default: prints to stdout)
#
# Example:
#   ./refresh_catalog.sh
#   ./refresh_catalog.sh vendor/hyva-themes/hyva-ui components.md
#

HYVA_UI_PATH="${1:-vendor/hyva-themes/hyva-ui}"
OUTPUT_FILE="$2"

if [ ! -d "$HYVA_UI_PATH/components" ]; then
    echo "Error: Hyva UI components directory not found at $HYVA_UI_PATH/components" >&2
    exit 1
fi

# Function to check if a component variant is CMS-based
is_cms_component() {
    local variant_path="$1"
    [ -f "$variant_path/src/cms-content.phtml" ]
}

# Function to extract first paragraph from README as description
get_description() {
    local readme="$1"
    if [ -f "$readme" ]; then
        # Get first non-badge, non-header paragraph
        sed -n '/^[^#\[!\[]/{p;q;}' "$readme" | head -1
    fi
}

# Generate the catalog
generate_catalog() {
    cat << 'HEADER'
# Hyva UI Components Catalog

This catalog is auto-generated by scanning the installed `vendor/hyva-themes/hyva-ui` package.
To refresh after updating hyva-ui, run: `scripts/refresh_catalog.sh`

HEADER

    echo "## Non-CMS Components (Template-Based)"
    echo ""
    echo "These components require copying template files to the theme directory."
    echo ""

    local non_cms_components=""
    local cms_components=""

    # Scan all component directories
    for component_dir in "$HYVA_UI_PATH/components"/*; do
        [ -d "$component_dir" ] || continue
        local component_name=$(basename "$component_dir")

        local has_non_cms=false
        local has_cms=false
        local variants=""
        local cms_variants=""

        # Scan variants
        for variant_dir in "$component_dir"/*; do
            [ -d "$variant_dir" ] || continue
            local variant_name=$(basename "$variant_dir")

            # Skip non-variant directories
            [ -d "$variant_dir/src" ] || continue

            local desc=$(get_description "$variant_dir/README.md")

            if is_cms_component "$variant_dir"; then
                has_cms=true
                cms_variants="$cms_variants\n- **$variant_name**: $desc"
            else
                has_non_cms=true
                variants="$variants\n- **$variant_name**: $desc"
            fi
        done

        # Add to appropriate section
        if [ "$has_non_cms" = true ]; then
            non_cms_components="$non_cms_components
### $component_name
$(echo -e "$variants")
"
        fi

        if [ "$has_cms" = true ]; then
            cms_components="$cms_components
### $component_name
$(echo -e "$cms_variants")
"
        fi
    done

    echo "$non_cms_components"

    echo ""
    echo "---"
    echo ""
    echo "## CMS Components (Excluded from this skill)"
    echo ""
    echo "These components have \`cms-content.phtml\` and are designed to be pasted into Magento CMS."
    echo "They require the Hyvä CMS Tailwind JIT module and are NOT handled by this skill."
    echo ""
    echo "$cms_components"

    echo ""
    echo "---"
    echo ""
    echo "## Plugins"
    echo ""
    echo "Utility plugins that provide additional functionality for certain components."
    echo ""

    for plugin_dir in "$HYVA_UI_PATH/plugins"/*; do
        [ -d "$plugin_dir" ] || continue
        local plugin_name=$(basename "$plugin_dir")
        local desc=$(get_description "$plugin_dir/README.md")
        echo "### $plugin_name"
        echo "$desc"
        echo ""
    done

    echo "---"
    echo ""
    echo "_Generated on: $(date '+%Y-%m-%d %H:%M:%S')_"
    echo "_Hyva UI path: $HYVA_UI_PATH_"
}

# Output to file or stdout
if [ -n "$OUTPUT_FILE" ]; then
    generate_catalog > "$OUTPUT_FILE"
    echo "Catalog written to: $OUTPUT_FILE"
else
    generate_catalog
fi
