<?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
    {
        Schema::table('configuracion', function (Blueprint $table) {
            $table->foreignId('sucursal_id')
                  ->nullable()
                  ->after('id')
                  ->constrained('sucursales')
                  ->cascadeOnDelete();

            // Eliminar unique simple en clave
            $table->dropUnique(['clave']);

            // Nuevo unique compuesto: clave + sucursal_id
            $table->unique(['clave', 'sucursal_id']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('configuracion', function (Blueprint $table) {
            $table->dropUnique(['clave', 'sucursal_id']);
            $table->dropForeign(['sucursal_id']);
            $table->dropColumn('sucursal_id');
            $table->unique('clave');
        });
    }
};
