#!/bin/bash
# =============================================================================
# MODPACK VERSION CHECKER - BUILD SCRIPT
# =============================================================================
#
# Executes automatically during `blueprint -build`
# Injects React components into Pterodactyl's frontend
#
# @author      Firefrost Gaming / Frostystyle <dev@firefrostgaming.com>
# @version     1.0.0
# =============================================================================

echo "=========================================="
echo "ModpackChecker Build Script v1.0.0"
echo "=========================================="

# Determine the extension source directory
# Blueprint may run from .blueprint/dev/ or .blueprint/extensions/modpackchecker/
if [ -d ".blueprint/extensions/modpackchecker/views" ]; then
    EXT_DIR=".blueprint/extensions/modpackchecker"
elif [ -d ".blueprint/dev/views" ]; then
    EXT_DIR=".blueprint/dev"
else
    echo "ERROR: Cannot find extension views directory"
    exit 1
fi

echo "Using extension directory: $EXT_DIR"

# ===========================================
# 1. CONSOLE WIDGET INJECTION (Right Column)
# ===========================================
echo ""
echo "--- Console Widget ---"

if [ -f "$EXT_DIR/views/server/wrapper.tsx" ]; then
    cp "$EXT_DIR/views/server/wrapper.tsx" resources/scripts/components/server/ModpackVersionCard.tsx
    echo "✓ Copied ModpackVersionCard.tsx"
else
    echo "⚠ wrapper.tsx not found, skipping console widget"
fi

# Inject into AfterInformation.tsx (right column, after stats)
AFTER_INFO="resources/scripts/blueprint/components/Server/Terminal/AfterInformation.tsx"
if [ -f "$AFTER_INFO" ]; then
    if ! grep -q "ModpackVersionCard" "$AFTER_INFO" 2>/dev/null; then
        # Add import after the blueprint/import comment
        sed -i '/\/\* blueprint\/import \*\//a import ModpackVersionCard from "@/components/server/ModpackVersionCard";' "$AFTER_INFO"
        # Add component inside the fragment after blueprint/react comment
        sed -i 's|{/\* blueprint/react \*/}|{/* blueprint/react */}\n      <ModpackVersionCard />|' "$AFTER_INFO"
        echo "✓ Injected ModpackVersionCard into AfterInformation.tsx"
    else
        echo "○ ModpackVersionCard already present in AfterInformation.tsx"
    fi
else
    echo "⚠ AfterInformation.tsx not found, skipping injection"
fi

# ===========================================
# 2. DASHBOARD BADGE INJECTION
# ===========================================
echo ""
echo "--- Dashboard Badge ---"

if [ -f "$EXT_DIR/views/dashboard/UpdateBadge.tsx" ]; then
    mkdir -p resources/scripts/components/dashboard
    cp "$EXT_DIR/views/dashboard/UpdateBadge.tsx" resources/scripts/components/dashboard/UpdateBadge.tsx
    echo "✓ Copied UpdateBadge.tsx"
else
    echo "⚠ UpdateBadge.tsx not found, skipping dashboard badge"
fi

# Inject into ServerRow.tsx (dashboard server list)
if ! grep -q "UpdateBadge" resources/scripts/components/dashboard/ServerRow.tsx 2>/dev/null; then
    sed -i '1i import UpdateBadge from "@/components/dashboard/UpdateBadge";' resources/scripts/components/dashboard/ServerRow.tsx
    # Targeted replacement: append badge after server name
    sed -i 's|{server.name}</p>|{server.name}<UpdateBadge serverUuid={server.uuid} /></p>|' resources/scripts/components/dashboard/ServerRow.tsx
    echo "✓ Injected UpdateBadge into ServerRow.tsx"
else
    echo "○ UpdateBadge already present in ServerRow.tsx"
fi

# ===========================================
# NOTE: Console Command (CheckModpackUpdates.php)
# ===========================================
# The PHP console command is automatically merged by Blueprint via
# conf.yml's `requests.app: "app"` setting. No manual copy needed.

echo ""
echo "=========================================="
echo "ModpackChecker injection complete!"
echo "=========================================="
echo ""
echo "Next steps:"
echo "  1. Run: yarn build:production"
echo "  2. Restart: systemctl restart php8.3-fpm"
echo "  3. Test cron: php artisan modpackchecker:check"
echo ""
