<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     * Creates the sms_messages table for tracking SMS communications.
     * 
     * Requirements: 2.1, 2.4, 2.8, 2.10, 8.2
     */
    public function up(): void
    {
        Schema::create('sms_messages', function (Blueprint $table) {
            // Primary key
            $table->id();

            // Who sent the SMS
            $table->foreignId('sender_id')
                ->constrained('users')
                ->onDelete('cascade')
                ->comment('User who initiated the SMS');

            // Recipients stored as JSON array
            // Format: [{"phone": "+254712345678", "name": "John Doe"}]
            $table->json('recipients')->nullable()->comment('JSON array of recipients');

            // Message content
            $table->text('message');

            // Template reference (optional)
            $table->foreignId('template_id')
                ->nullable()
                ->constrained('sms_templates')
                ->nullOnDelete()
                ->comment('Template used if applicable');

            // Sender ID for Africa's Talking (permanent Sender ID)
            $table->string('sender_id_name', 50)->nullable()->comment('Custom sender ID for delivery');

            // Scheduling
            $table->timestamp('scheduled_at')->nullable()->comment('When SMS should be sent (for scheduled messages)');
            $table->timestamp('sent_at')->nullable()->comment('Actual send timestamp');

            // Status tracking
            $table->enum('status', ['draft', 'scheduled', 'sending', 'sent', 'failed', 'partial'])
                ->default('draft')
                ->comment('Current delivery status');

            // Africa's Talking response data
            $table->string('message_id', 191)->nullable()->comment('Africa\'s Talking message ID');
            $table->decimal('cost', 10, 4)->default(0)->comment('Total cost of sending');

            // Delivery reports stored as JSON
            // Format: [{"phone": "+254712345678", "status": "Delivered", "delivered_at": "2024-01-01 10:00:00"}]
            $table->json('delivery_reports')->nullable()->comment('Delivery status per recipient');

            // Error tracking
            $table->text('error_message')->nullable()->comment('Error message if failed');

            // Recipient selection method used
            $table->enum('recipient_type', ['manual', 'csv', 'role_group', 'mixed'])
                ->default('manual')
                ->comment('How recipients were selected');

            // Role group reference (if applicable)
            $table->string('target_role', 100)->nullable()->comment('Target role if role-based selection');

            // Standard timestamps
            $table->timestamps();
            $table->softDeletes();

            // Indexes for performance
            $table->index('status');
            $table->index('scheduled_at');
            $table->index('sent_at');
            $table->index(['sender_id', 'status']);
            $table->index('created_at');
        });
    }

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