<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        if (!Schema::hasTable('shipping_fees')) return;
        
        // Ajouter les index avec try/catch pour éviter les erreurs si déjà existants
        try {
            Schema::table('shipping_fees', function (Blueprint $table) {
                $table->index('country', 'shipping_fees_country_index');
            });
        } catch (\Exception $e) {}
        
        try {
            Schema::table('shipping_fees', function (Blueprint $table) {
                $table->index('city', 'shipping_fees_city_index');
            });
        } catch (\Exception $e) {}
        
        try {
            Schema::table('shipping_fees', function (Blueprint $table) {
                $table->index('is_active', 'shipping_fees_is_active_index');
            });
        } catch (\Exception $e) {}
        
        try {
            Schema::table('shipping_fees', function (Blueprint $table) {
                $table->index(['country', 'city'], 'shipping_fees_country_city_index');
            });
        } catch (\Exception $e) {}
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('shipping_fees', function (Blueprint $table) {
            if (!Schema::hasColumn('shipping_fees', 'shipping_fees_country_index')) { $table->dropIndex('shipping_fees_country_index'); }
            if (!Schema::hasColumn('shipping_fees', 'shipping_fees_city_index')) { $table->dropIndex('shipping_fees_city_index'); }
            if (!Schema::hasColumn('shipping_fees', 'shipping_fees_is_active_index')) { $table->dropIndex('shipping_fees_is_active_index'); }
            if (!Schema::hasColumn('shipping_fees', 'shipping_fees_country_city_index')) { $table->dropIndex('shipping_fees_country_city_index'); }
        });
    }
};

