<?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::create('solicitor_phone_numbers', function (Blueprint $table) {
            $table->id();

            $table->foreignId('solicitor_id')->constrained()->cascadeOnDelete();
            $table->foreignId('phone_number_type_id')->constrained()->restrictOnDelete();

            $table->string('label')->nullable();
            $table->string('phone_number', 50)->unique();
            $table->string('normalized_phone_number', 50)->unique();

            $table->string('provider', 50)->default('twilio');

            $table->string('twilio_account_sid')->nullable();
            $table->string('twilio_subaccount_sid')->nullable()->index();
            $table->string('twilio_phone_number_sid')->nullable()->index();

            $table->boolean('is_active')->default(true)->index();
            $table->boolean('is_primary')->default(false)->index();

            $table->json('capabilities')->nullable(); // ["voice"], ["voice","sms"]
            $table->json('meta')->nullable();

            $table->timestamps();

            $table->index(['solicitor_id', 'phone_number_type_id']);
            $table->index(['solicitor_id', 'is_active']);
            $table->unique(['solicitor_id', 'phone_number_type_id']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('solicitor_phone_numbers');
    }
};
