<?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_templates table for reusable SMS templates.
     * 
     * Requirements: 2.3, 3.8
     */
    public function up(): void
    {
        Schema::create('sms_templates', function (Blueprint $table) {
            // Primary key
            $table->id();

            // Template identification (respecting 191 char limit for indexed columns)
            $table->string('name', 191)->comment('Human-readable template name');
            $table->string('slug', 191)->unique()->comment('URL-friendly identifier');

            // Template content
            $table->text('content')->comment('SMS template content with optional placeholders');

            // Template description for admin reference
            $table->text('description')->nullable()->comment('Description of when to use this template');

            // Active status
            $table->boolean('is_active')
                ->default(true)
                ->comment('Whether template is available for use');

            // Who created this template
            $table->foreignId('created_by')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete()
                ->comment('User who created the template');

            // Usage tracking
            $table->integer('usage_count')->default(0)->comment('Number of times template has been used');

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

            // Indexes for performance
            $table->index('is_active');
            $table->index('slug');
        });
    }

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