<?php

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

/**
 * Whitelisted outbound SMS sender identities (numbers + alpha tags).
 *
 * Distinct from solicitor_phone_numbers, which is reserved for the dialler /
 * voice domain.  Only entries in this table can be used as the "from" identity
 * on outbound SMS messages sent through the Contact Hub.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('solicitor_sms_senders', function (Blueprint $t) {
            $t->id();
            $t->foreignId('solicitor_id')->constrained()->cascadeOnDelete();

            $t->string('label');                    // friendly name shown in dropdowns
            $t->string('type', 16);                 // 'number' | 'alpha_tag'
            $t->string('value');                    // raw value as authored
            $t->string('normalized_value')->nullable(); // E.164 form for numbers, null for alpha tags

            $t->string('provider', 24)->default('twilio');
            $t->string('twilio_account_sid')->nullable();
            $t->string('twilio_subaccount_sid')->nullable()->index();
            $t->string('twilio_phone_number_sid')->nullable()->index();

            $t->boolean('is_active')->default(true)->index();
            $t->boolean('is_default')->default(false)->index();
            $t->boolean('supports_replies')->default(true);  // false for alpha tags

            $t->json('meta')->nullable();
            $t->timestamps();

            $t->index(['solicitor_id', 'is_active']);
            $t->unique(['solicitor_id', 'type', 'value'], 'solicitor_sms_senders_unique');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('solicitor_sms_senders');
    }
};
