#!/bin/bash

echo "======================================"
echo "   Laravel Cache Clear Script"
echo "======================================"
echo ""

# Get the directory where the script is located
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

cd "$SCRIPT_DIR"

echo "Current directory: $(pwd)"
echo ""

# Check if artisan exists
if [ ! -f "artisan" ]; then
    echo "❌ Error: artisan file not found!"
    echo "Please run this script from your Laravel root directory."
    exit 1
fi

echo "Clearing Laravel caches..."
echo ""

# Clear configuration cache
echo "→ Clearing config cache..."
php artisan config:clear
if [ $? -eq 0 ]; then
    echo "  ✓ Config cache cleared"
else
    echo "  ✗ Failed to clear config cache"
fi

# Clear route cache
echo "→ Clearing route cache..."
php artisan route:clear
if [ $? -eq 0 ]; then
    echo "  ✓ Route cache cleared"
else
    echo "  ✗ Failed to clear route cache"
fi

# Clear view cache
echo "→ Clearing view cache..."
php artisan view:clear
if [ $? -eq 0 ]; then
    echo "  ✓ View cache cleared"
else
    echo "  ✗ Failed to clear view cache"
fi

# Clear application cache
echo "→ Clearing application cache..."
php artisan cache:clear
if [ $? -eq 0 ]; then
    echo "  ✓ Application cache cleared"
else
    echo "  ✗ Failed to clear application cache"
fi

# Clear all optimization
echo "→ Clearing all optimization..."
php artisan optimize:clear
if [ $? -eq 0 ]; then
    echo "  ✓ All optimization cleared"
else
    echo "  ✗ Failed to clear optimization"
fi

echo ""
echo "→ Recreating optimized files..."
php artisan config:cache
php artisan route:cache
php artisan view:cache

echo ""
echo "======================================"
echo "   Cache clearing complete!"
echo "======================================"
echo ""
echo "Now test your application at:"
echo "https://i-syura.cscentralhub.com/manager/my-meetings/201/catatan"
echo ""
