<?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_configurations table to store Africa's Talking API credentials.
     * All sensitive fields (api_key, username) are encrypted at rest using Laravel's
     * encryption system.
     * 
     * 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_configurations', function (Blueprint $table) {
            // Primary key
            $table->id();
            
            // API credentials (encrypted at rest)
            // Using text type to accommodate encrypted strings which can be longer
            $table->text('api_key')->comment('Encrypted Africa\'s Talking API key');
            $table->text('username')->comment('Encrypted Africa\'s Talking username');
            
            // Sender ID for outgoing messages
            // Limited to 11 characters for alphanumeric sender IDs as per Africa's Talking spec
            $table->string('sender_id', 11)->comment('Sender ID for outgoing SMS');
            
            // Environment setting (sandbox or production)
            $table->enum('environment', ['sandbox', 'production'])
                  ->default('sandbox')
                  ->comment('API environment: sandbox for testing, production for live');
            
            // Active status flag
            $table->boolean('is_active')
                  ->default(true)
                  ->comment('Whether this configuration is currently active');
            
            // Timestamps
            $table->timestamps();
            
            // Soft deletes for configuration history
            $table->softDeletes();
            
            // Indexes for performance
            $table->index('is_active');
            $table->index('environment');
            $table->index('created_at');
        });
    }

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