#!/bin/bash
#
# Hotfix Navigation URLs
#
# Patches nav-defaults.ts with corrected AMSOIL URLs.
# Run this on production to fix new dealer signups immediately,
# then rebuild the app.
#
# Usage:
#   # Preview changes (dry run)
#   ./scripts/hotfix-nav-urls.sh --dry-run
#
#   # Apply changes
#   ./scripts/hotfix-nav-urls.sh
#
#   # Then rebuild
#   npm run build
#
# This script patches:
#   - business-opportunities: /become-an-amsoil-dealer → /business-opportunities/
#   - business-opportunities.start-business: /become-an-amsoil-dealer → /lander/join/
#   - offers: /offers → /offers/pc/
#   - offers.preferred-customer: /preferred-customer → /offers/pc/
#   - offers.free-shipping: /free-shipping → /offers/pc/
#

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
NAV_DEFAULTS="$PROJECT_ROOT/lib/cms/nav-defaults.ts"

DRY_RUN=false
if [[ "$1" == "--dry-run" ]]; then
    DRY_RUN=true
fi

echo "============================================================"
if $DRY_RUN; then
    echo "🔍 DRY RUN MODE - No changes will be made"
else
    echo "🚀 LIVE MODE - Changes will be applied"
fi
echo "============================================================"
echo ""

# Check file exists
if [[ ! -f "$NAV_DEFAULTS" ]]; then
    echo "ERROR: nav-defaults.ts not found at: $NAV_DEFAULTS"
    exit 1
fi

echo "Target file: $NAV_DEFAULTS"
echo ""

# Define replacements (old URL -> new URL)
# Using | as delimiter since URLs contain /
declare -A REPLACEMENTS=(
    ["https://www.amsoil.com/become-an-amsoil-dealer"]="SPLIT_REPLACEMENT"
    ["https://www.amsoil.com/offers']"]="https://www.amsoil.com/offers/pc/']"
    ["https://www.amsoil.com/preferred-customer"]="https://www.amsoil.com/offers/pc/"
    ["https://www.amsoil.com/free-shipping"]="https://www.amsoil.com/offers/pc/"
)

# Show current state
echo "📊 Current URLs in file:"
echo ""
grep -n "externalUrl:" "$NAV_DEFAULTS" | grep -E "(become-an-amsoil-dealer|/offers'|preferred-customer|free-shipping)" || echo "  (no broken URLs found)"
echo ""

if $DRY_RUN; then
    echo "📝 Changes that would be made:"
    echo ""
    echo "  1. 'business-opportunities' parent:"
    echo "     - https://www.amsoil.com/become-an-amsoil-dealer"
    echo "     + https://www.amsoil.com/business-opportunities/"
    echo ""
    echo "  2. 'business-opportunities.start-business' child:"
    echo "     - https://www.amsoil.com/become-an-amsoil-dealer"
    echo "     + https://www.amsoil.com/lander/join/"
    echo ""
    echo "  3. 'offers' parent:"
    echo "     - https://www.amsoil.com/offers"
    echo "     + https://www.amsoil.com/offers/pc/"
    echo ""
    echo "  4. 'offers.preferred-customer' child:"
    echo "     - https://www.amsoil.com/preferred-customer"
    echo "     + https://www.amsoil.com/offers/pc/"
    echo ""
    echo "  5. 'offers.free-shipping' child:"
    echo "     - https://www.amsoil.com/free-shipping"
    echo "     + https://www.amsoil.com/offers/pc/"
    echo ""
    echo "============================================================"
    echo "🔍 DRY RUN COMPLETE - No changes were made"
    echo "   Run without --dry-run to apply changes"
    echo "============================================================"
    exit 0
fi

# Apply changes
# Note: We need to be careful because 'become-an-amsoil-dealer' appears twice
# with different replacements. We use context-aware sed patterns.

echo "📝 Applying changes..."
echo ""

# Create backup
cp "$NAV_DEFAULTS" "$NAV_DEFAULTS.bak"
echo "  ✓ Backup created: $NAV_DEFAULTS.bak"

# 1. Fix 'business-opportunities' parent (line with defaultKey: 'business-opportunities' followed by externalUrl)
# This is the first occurrence - replace with /business-opportunities/
sed -i "/defaultKey: 'business-opportunities',/{n;n;s|https://www.amsoil.com/become-an-amsoil-dealer|https://www.amsoil.com/business-opportunities/|}" "$NAV_DEFAULTS"
echo "  ✓ Fixed: business-opportunities → /business-opportunities/"

# 2. Fix 'business-opportunities.start-business' child
# This is the second occurrence - replace with /lander/join/
sed -i "/defaultKey: 'business-opportunities.start-business',/{n;n;s|https://www.amsoil.com/become-an-amsoil-dealer|https://www.amsoil.com/lander/join/|}" "$NAV_DEFAULTS"
echo "  ✓ Fixed: business-opportunities.start-business → /lander/join/"

# 3. Fix 'offers' parent
sed -i "s|https://www.amsoil.com/offers',|https://www.amsoil.com/offers/pc/',|g" "$NAV_DEFAULTS"
echo "  ✓ Fixed: offers → /offers/pc/"

# 4. Fix 'offers.preferred-customer' child
sed -i "s|https://www.amsoil.com/preferred-customer|https://www.amsoil.com/offers/pc/|g" "$NAV_DEFAULTS"
echo "  ✓ Fixed: offers.preferred-customer → /offers/pc/"

# 5. Fix 'offers.free-shipping' child
sed -i "s|https://www.amsoil.com/free-shipping|https://www.amsoil.com/offers/pc/|g" "$NAV_DEFAULTS"
echo "  ✓ Fixed: offers.free-shipping → /offers/pc/"

echo ""

# Verify changes
echo "📊 Updated URLs in file:"
echo ""
grep -n "externalUrl:" "$NAV_DEFAULTS" | grep -E "(business-opportunities/|lander/join|offers/pc)" | head -5
echo ""

echo "============================================================"
echo "✅ HOTFIX COMPLETE"
echo ""
echo "Next steps:"
echo "  1. Rebuild the app:  npm run build"
echo "  2. Restart the app"
echo ""
echo "To revert: cp $NAV_DEFAULTS.bak $NAV_DEFAULTS"
echo "============================================================"

