<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('provider_specialities', function (Blueprint $table) {
            $table->id();
            $table->foreignId('provider_id')->constrained('providers')->onDelete('cascade');
            $table->foreignId('speciality_id')->constrained('specialities')->onDelete('cascade');
            $table->timestamps();
            $table->softDeletes();

            $table->index('provider_id');
            $table->index('speciality_id');
        });

        // Unique constraint considerando soft deletes
        DB::statement('CREATE UNIQUE INDEX provider_speciality_unique ON provider_specialities(provider_id, speciality_id) WHERE deleted_at IS NULL');
    }

    public function down(): void
    {
        DB::statement('DROP INDEX IF EXISTS provider_speciality_unique');
        Schema::dropIfExists('provider_specialities');
    }
};
