<?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_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('call_log_id')->nullable()->constrained()->onDelete('set null');
            $table->bigInteger('local_id')->nullable(); // ID from Android app
            $table->string('phone_number');
            $table->text('message');
            $table->enum('status', ['PENDING', 'SENT', 'DELIVERED', 'FAILED'])->default('PENDING');
            $table->string('error_message')->nullable();
            $table->timestamp('sent_at');
            $table->timestamps();

            $table->index('phone_number');
            $table->index('sent_at');
            $table->index('status');
            $table->index(['user_id', 'sent_at']);
        });
    }

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