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

            // Polymorphic owner
            $table->morphs('sendable');

            // SMS fields
            $table->string('to_number');
            $table->string('from_number')->nullable();
            $table->text('message');

            // Scheduling
            $table->timestamp('scheduled_for')->nullable();
            $table->timestamp('sent_at')->nullable();

            // Status & retries
            $table->string('status')->default('pending');
            $table->unsignedTinyInteger('attempts')->default(0);
            $table->timestamp('last_attempt_at')->nullable();

            // Provider details
            $table->string('provider')->nullable();
            $table->string('provider_message_id')->nullable();
            $table->json('provider_response')->nullable();

            // Error handling
            $table->text('error_message')->nullable();

            $table->timestamps();

            $table->index(['status', 'scheduled_for']);
        });
    }

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