<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\Database\Migrations\TenantMigration;

return new class extends TenantMigration
{
    public function up()
    {
        Schema::create('sms_messages', function (Blueprint $t) {
            $t->id();
            $t->string('subject');
            $t->longText('content');
            $t->enum('status', ['draft', 'scheduled', 'processing', 'sent', 'failed', 'canceled'])->default('draft')->index();
            $t->dateTime('schedule_at')->nullable()->index();
            $t->dateTime('sent_at')->nullable();
            $t->unsignedBigInteger('created_by')->index();
            $t->timestamps();
        });

        Schema::create('sms_message_recipients', function (Blueprint $t) {
            $t->id();
            $t->foreignId('sms_message_id')->constrained('sms_messages')->cascadeOnDelete();
            $t->foreignId('member_id')->nullable()->constrained('members')->nullOnDelete();
            $t->string('phone');
            $t->enum('status', ['pending', 'sent', 'failed', 'bounced', 'skipped'])->default('pending')->index();
            $t->text('error_message')->nullable();
            $t->dateTime('sent_at')->nullable();
            $t->timestamps();

            $t->index(['sms_message_id', 'status']);
        });
    }

    public function down()
    {
        Schema::dropIfExists('sms_message_recipients');
        Schema::dropIfExists('sms_messages');
    }
};
