#!/bin/bash
# Migration script to merge meta.toml content into markdown frontmatter
# Preserves existing frontmatter values, adds missing ones from meta.toml

set -e

echo "🔄 Starting frontmatter migration..."

# Function to process a single markdown file and its corresponding meta.toml
process_markdown_file() {
    local md_file="$1"
    local meta_file="$2"

    if [[ ! -f "$md_file" ]] || [[ ! -f "$meta_file" ]]; then
        return 0
    fi

    echo "📝 Processing: $md_file"

    # Extract existing frontmatter
    local frontmatter_end=$(grep -n "^---$" "$md_file" | sed -n '2p' | cut -d: -f1)
    if [[ -z "$frontmatter_end" ]]; then
        echo "  ⚠️  No frontmatter found, skipping"
        return 0
    fi

    # Read meta.toml values
    local meta_slug=""
    local meta_title=""
    local meta_published=""
    local meta_sort_order=""
    local meta_description=""
    local meta_css_class=""
    local meta_css_style=""

    if [[ -f "$meta_file" ]]; then
        meta_slug=$(grep '^slug = ' "$meta_file" | sed 's/slug = "\(.*\)"/\1/')
        meta_title=$(grep '^title = ' "$meta_file" | sed 's/title = "\(.*\)"/\1/')
        meta_published=$(grep '^published = ' "$meta_file" | sed 's/published = \(.*\)/\1/')
        meta_sort_order=$(grep '^sort_order = ' "$meta_file" | sed 's/sort_order = \(.*\)/\1/')
        meta_description=$(grep '^description = ' "$meta_file" | sed 's/description = "\(.*\)"/\1/')
        meta_css_class=$(grep '^css_class = ' "$meta_file" | sed 's/css_class = "\(.*\)"/\1/')
        meta_css_style=$(grep '^css_style = ' "$meta_file" | sed 's/css_style = "\(.*\)"/\1/')
    fi

    # Create backup
    cp "$md_file" "$md_file.backup"

    # Extract current frontmatter content (between the --- markers)
    local current_frontmatter=$(sed -n '2,'$((frontmatter_end-1))'p' "$md_file")

    # Check what fields exist in current frontmatter
    local has_category_description=$(echo "$current_frontmatter" | grep -q '^category_description:' && echo "yes" || echo "no")
    local has_category_published=$(echo "$current_frontmatter" | grep -q '^category_published:' && echo "yes" || echo "no")
    local has_sort_order=$(echo "$current_frontmatter" | grep -q '^sort_order:' && echo "yes" || echo "no")
    local has_css_class=$(echo "$current_frontmatter" | grep -q '^css_class:' && echo "yes" || echo "no")
    local has_css_style=$(echo "$current_frontmatter" | grep -q '^css_style:' && echo "yes" || echo "no")

    # Build additional frontmatter fields from meta.toml (only if not already present)
    local additional_fields=""

    if [[ "$has_category_description" == "no" && -n "$meta_description" ]]; then
        additional_fields="${additional_fields}category_description: \"$meta_description\"\n"
    fi

    if [[ "$has_category_published" == "no" && -n "$meta_published" ]]; then
        additional_fields="${additional_fields}category_published: $meta_published\n"
    fi

    if [[ "$has_sort_order" == "no" && -n "$meta_sort_order" ]]; then
        additional_fields="${additional_fields}sort_order: $meta_sort_order\n"
    fi

    if [[ "$has_css_class" == "no" && -n "$meta_css_class" ]]; then
        additional_fields="${additional_fields}css_class: \"$meta_css_class\"\n"
    fi

    if [[ "$has_css_style" == "no" && -n "$meta_css_style" && "$meta_css_style" != '""' ]]; then
        additional_fields="${additional_fields}css_style: \"$meta_css_style\"\n"
    fi

    # Only modify if we have additional fields to add
    if [[ -n "$additional_fields" ]]; then
        echo "  ✅ Adding fields: category_description, category_published, sort_order, css_class"

        # Create new file with enhanced frontmatter
        {
            echo "---"
            echo "$current_frontmatter"
            echo -e "$additional_fields"
            echo "---"
            tail -n +$((frontmatter_end+1)) "$md_file"
        } > "$md_file.tmp"

        mv "$md_file.tmp" "$md_file"
    else
        echo "  ℹ️  No additional fields needed"
        rm "$md_file.backup"
    fi
}

# Find all markdown files that have corresponding meta.toml
find site/content -name "*.md" -type f | while read -r md_file; do
    # Get directory of the markdown file
    dir=$(dirname "$md_file")
    meta_file="$dir/meta.toml"

    if [[ -f "$meta_file" ]]; then
        process_markdown_file "$md_file" "$meta_file"
    fi
done

echo "✅ Frontmatter migration completed!"
echo "📋 Backup files created with .backup extension"
echo "🔍 Review changes before removing meta.toml files"
