#!/bin/bash

# Site-Info Template Setup Script
# This script initializes the site-info template system

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TEMPLATES_DIR="$SCRIPT_DIR"

echo "🎨 Site-Info Template System Setup"
echo "=================================="
echo ""

# Check if templates already exist
if [ -f "$TEMPLATES_DIR/documentation/base.tera" ]; then
    echo "✅ Templates are already set up at: $TEMPLATES_DIR"
    echo ""
    echo "📁 Template structure:"
    find "$TEMPLATES_DIR" -name "*.tera" -o -name "*.md" | sort | sed 's|^|   |'
    echo ""
    echo "ℹ️  Templates are ready for use. No action needed."
    exit 0
fi

echo "🚨 WARNING: Templates directory exists but templates are missing!"
echo "   Directory: $TEMPLATES_DIR"
echo ""
echo "This usually happens when:"
echo "  - Templates were moved from source but files are missing"
echo "  - Git didn't track the template files"
echo "  - Template files were accidentally deleted"
echo ""

# Check if we're in the right place
if [ ! -f "$SCRIPT_DIR/../../../crates/site-info/Cargo.toml" ]; then
    echo "❌ Error: This script must be run from the correct project directory"
    echo "   Expected: site/templates/site-info/"
    echo "   Current:  $SCRIPT_DIR"
    exit 1
fi

echo "🔧 Template Recovery Options:"
echo ""
echo "1. Restore from crate source (if available):"
echo "   cp -r ../../../crates/site-info/src/templates/* ."
echo ""
echo "2. Initialize from scratch:"
echo "   Create templates manually using the documentation in README.md"
echo ""
echo "3. Check git history:"
echo "   git log --follow -- site/templates/site-info/"
echo ""

# Offer to restore from crate if source still exists
CRATE_TEMPLATES="../../../crates/site-info/src/templates"
if [ -d "$CRATE_TEMPLATES" ]; then
    echo "✨ Found template source in crate directory!"
    echo ""
    read -p "Restore templates from crate source? [y/N]: " -n 1 -r
    echo ""

    if [[ $REPLY =~ ^[Yy]$ ]]; then
        echo "📋 Copying templates from crate source..."

        # Copy templates while preserving structure
        if [ -d "$CRATE_TEMPLATES/documentation" ]; then
            cp -r "$CRATE_TEMPLATES/documentation" "$TEMPLATES_DIR/"
            echo "   ✓ Copied documentation templates"
        fi

        if [ -d "$CRATE_TEMPLATES/partials" ]; then
            cp -r "$CRATE_TEMPLATES/partials" "$TEMPLATES_DIR/"
            echo "   ✓ Copied partial templates"
        fi

        if [ -d "$CRATE_TEMPLATES/i18n" ]; then
            cp -r "$CRATE_TEMPLATES/i18n" "$TEMPLATES_DIR/"
            echo "   ✓ Copied i18n template directories"
        fi

        echo ""
        echo "✅ Templates restored successfully!"
        echo ""
        echo "📁 Template structure:"
        find "$TEMPLATES_DIR" -name "*.tera" | sort | sed 's|^|   |'
        echo ""
        echo "🎯 Next steps:"
        echo "   1. Test the build: cargo build"
        echo "   2. Generate docs: cargo run --bin site-info-generator"
        echo "   3. Commit templates: git add site/templates/site-info/"

        exit 0
    fi
fi

echo ""
echo "📖 For manual template creation, see:"
echo "   site/templates/site-info/README.md"
echo ""
echo "🔗 Template examples and documentation:"
echo "   https://keats.github.io/tera/docs/"
echo ""

exit 1
