#!/bin/bash

# Simple Content Manager (Bash)
# Uses the enhanced Rust content_processor for all operations

set -euo pipefail

# Show help information
show_help() {
    cat << 'EOF'
Simple Content Manager (Bash)
Convenient interface for content operations using the Rust content_processor

USAGE:
  ./content-manager.sh COMMAND [OPTIONS]

COMMANDS:
  build                     Build all content
  build-type TYPE           Build specific content type (blog, recipes)
  build-lang LANG           Build specific language (en, es)
  build-category CATEGORY   Build specific category
  build-file FILE           Build specific file (supports glob patterns)
  watch                     Watch for changes and auto-rebuild
  clean-build               Clean output and rebuild all
  help                      Show this help message

EXAMPLES:

  # Build all content
  ./content-manager.sh build

  # Build only blog content
  ./content-manager.sh build-type blog

  # Build only English content
  ./content-manager.sh build-lang en

  # Build specific category
  ./content-manager.sh build-category rust

  # Build specific file
  ./content-manager.sh build-file "blog/en/rust/rust-web-development-2024.md"

  # Build files with pattern
  ./content-manager.sh build-file "blog/en/*/rust-*.md"

  # Watch for changes
  ./content-manager.sh watch

  # Clean and rebuild
  ./content-manager.sh clean-build

CONTENT PROCESSOR OPTIONS:
  The underlying Rust content_processor supports these options:
  --content-type TYPE       Process specific content type
  --language LANG           Process specific language
  --category CATEGORY       Process specific category
  --file FILE              Process specific file(s) with glob support
  --watch                  Watch mode for hot reloading

GENERATED FILES:
  post-name.html           Markdown content converted to HTML
  post-name.json           Frontmatter data in JSON format
  index.json              Collection index with all posts and metadata

NOTES:
  - All operations use the fast Rust content_processor
  - Automatically copies generated content to server runtime directory
  - Respects project configuration-driven architecture
  - No hardcoded paths or content types
EOF
}

# Run content processor with given arguments
run_processor() {
    local cmd=("cargo" "run" "--features=content-static" "--bin" "content_processor" "--")
    cmd+=("$@")

    echo "🔧 Running: ${cmd[*]}"

    if "${cmd[@]}"; then
        echo "✅ Content processing completed!"

        # Copy to server directory
        local public_dir="public/r"
        local server_dir="target/site/r"

        echo "📋 Copying to server directory..."
        mkdir -p "$server_dir"
        if [[ -d "$public_dir" ]]; then
            cp -r "$public_dir"/* "$server_dir/" 2>/dev/null || true
            echo "✅ Content copied to server directory"
        fi
    else
        echo "❌ Content processing failed!"
        exit 1
    fi
}

# Clean output directory
clean_output() {
    local output_dir="public/r"
    if [[ -d "$output_dir" ]]; then
        echo "🧹 Cleaning output directory: $output_dir"
        rm -rf "$output_dir"
    fi
    mkdir -p "$output_dir"
}

# Main function
main() {
    local command="${1:-}"

    if [[ -z "$command" ]] || [[ "$command" == "help" ]] || [[ "$command" == "--help" ]] || [[ "$command" == "-h" ]]; then
        show_help
        exit 0
    fi

    case "$command" in
        "build")
            echo "🚀 Building all content..."
            run_processor
            ;;
        "build-type")
            if [[ $# -lt 2 ]]; then
                echo "❌ build-type requires a content type argument"
                echo "   Example: ./content-manager.sh build-type blog"
                exit 1
            fi
            local content_type="$2"
            echo "🎯 Building $content_type content..."
            run_processor "--content-type" "$content_type"
            ;;
        "build-lang")
            if [[ $# -lt 2 ]]; then
                echo "❌ build-lang requires a language argument"
                echo "   Example: ./content-manager.sh build-lang en"
                exit 1
            fi
            local language="$2"
            echo "🌍 Building $language content..."
            run_processor "--language" "$language"
            ;;
        "build-category")
            if [[ $# -lt 2 ]]; then
                echo "❌ build-category requires a category argument"
                echo "   Example: ./content-manager.sh build-category rust"
                exit 1
            fi
            local category="$2"
            echo "🏷️ Building $category category..."
            run_processor "--category" "$category"
            ;;
        "build-file")
            if [[ $# -lt 2 ]]; then
                echo "❌ build-file requires a file pattern argument"
                echo "   Example: ./content-manager.sh build-file \"blog/en/rust/*.md\""
                exit 1
            fi
            local file_pattern="$2"
            echo "📄 Building file pattern: $file_pattern"
            run_processor "--file" "$file_pattern"
            ;;
        "watch")
            echo "👀 Starting watch mode..."
            run_processor "--watch"
            ;;
        "clean-build")
            echo "🧹 Clean build - removing old content..."
            clean_output
            run_processor
            ;;
        *)
            echo "❌ Unknown command: $command"
            echo "Use 'help' to see available commands"
            exit 1
            ;;
    esac
}

# Run main function with all arguments
main "$@"
