<?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_services_types', function (Blueprint $table) {
            $table->id();
            $table->foreignId('provider_id')->constrained('providers')->onDelete('cascade');
            $table->foreignId('service_type_id')->constrained('service_types')->onDelete('cascade');
            $table->timestamps();
            $table->softDeletes();

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

        DB::statement('CREATE UNIQUE INDEX provider_service_type_unique ON provider_services_types(provider_id, service_type_id) WHERE deleted_at IS NULL');
    }

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