<?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_logs table to store all SMS API interactions for debugging,
     * auditing, and analytics purposes.
     * 
     * MySQL 5.7 Compatibility:
     * - All string columns respect the 191 character limit for indexed columns
     * - Uses InnoDB engine for transaction support
     * - utf8mb4 character set for full Unicode support
     */
    public function up(): void
    {
        Schema::create('sms_logs', function (Blueprint $table) {
            // Primary key
            $table->id();
            
            // Recipient phone number
            $table->string('recipient', 20)
                  ->comment('Phone number of the SMS recipient');
            
            // Message content
            $table->text('message')
                  ->comment('Content of the SMS message');
            
            // Message length for billing/statistics
            $table->unsignedInteger('message_length')
                  ->default(0)
                  ->comment('Character count of the message');
            
            // SMS segment count (for messages over 160 characters)
            $table->unsignedTinyInteger('segment_count')
                  ->default(1)
                  ->comment('Number of SMS segments used');
            
            // Current status of the SMS
            $table->enum('status', ['pending', 'sent', 'failed', 'delivered', 'undelivered'])
                  ->default('pending')
                  ->comment('Current status of the SMS');
            
            // Message ID returned by Africa's Talking API
            $table->string('message_id', 191)
                  ->nullable()
                  ->comment('Unique message ID from Africa\'s Talking API');
            
            // Full API response for debugging
            $table->json('response_body')
                  ->nullable()
                  ->comment('Full JSON response from Africa\'s Talking API');
            
            // Error message if failed
            $table->text('error_message')
                  ->nullable()
                  ->comment('Error message if SMS failed');
            
            // Execution time in milliseconds for performance monitoring
            $table->unsignedInteger('execution_time_ms')
                  ->default(0)
                  ->comment('Time taken to send SMS in milliseconds');
            
            // Cost of the SMS (for analytics)
            $table->decimal('cost', 10, 4)
                  ->nullable()
                  ->comment('Cost of the SMS in applicable currency');
            
            // Associated configuration (for tracking which config was used)
            $table->foreignId('sms_configuration_id')
                  ->nullable()
                  ->constrained('sms_configurations')
                  ->onDelete('set null')
                  ->comment('Reference to the SMS configuration used');
            
            // User who initiated the SMS (for auditing)
            $table->foreignId('user_id')
                  ->nullable()
                  ->constrained('users')
                  ->onDelete('set null')
                  ->comment('User who initiated the SMS');
            
            // Timestamp for when the SMS was created
            $table->timestamp('created_at')
                  ->useCurrent()
                  ->comment('When the SMS log was created');
            
            // No updated_at needed as logs are immutable
            
            // Indexes for performance
            $table->index('recipient');
            $table->index('status');
            $table->index('message_id');
            $table->index('created_at');
            $table->index(['status', 'created_at']);
            $table->index('sms_configuration_id');
            $table->index('user_id');
        });
    }

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