<?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 chat_messages table for individual chat messages.
     * 
     * Requirements: 4.4, 8.3
     */
    public function up(): void
    {
        Schema::create('chat_messages', function (Blueprint $table) {
            // Primary key
            $table->id();

            // Conversation reference
            $table->foreignId('conversation_id')
                ->constrained('chat_conversations')
                ->onDelete('cascade')
                ->comment('The conversation this message belongs to');

            // Sender information
            $table->enum('sender_type', ['agent', 'visitor', 'system'])
                ->default('visitor')
                ->comment('Who sent the message');

            $table->foreignId('sender_id')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete()
                ->comment('User ID if sent by agent');

            // Sender name for display (cached for performance)
            $table->string('sender_name', 191)->nullable()->comment('Display name of sender');

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

            // Message type
            $table->enum('type', ['text', 'file', 'image', 'system', 'auto'])
                ->default('text')
                ->comment('Type of message content');

            // For file/image attachments
            $table->string('file_path', 500)->nullable()->comment('Path to attached file');
            $table->string('file_name', 191)->nullable()->comment('Original file name');
            $table->string('file_mime', 100)->nullable()->comment('File MIME type');
            $table->integer('file_size')->default(0)->comment('File size in bytes');

            // Delivery tracking
            $table->boolean('is_delivered')->default(false)->comment('Whether message was delivered');
            $table->boolean('is_read')->default(false)->comment('Whether message was read');
            $table->timestamp('delivered_at')->nullable();
            $table->timestamp('read_at')->nullable();

            // Internal notes (only visible to agents)
            $table->boolean('is_internal')->default(false)->comment('Internal note not shown to visitor');

            // System message type (for automated messages)
            $table->string('system_event', 100)->nullable()->comment('Event type if system message');

            // Reply to specific message (for threaded conversations)
            $table->foreignId('reply_to_id')
                ->nullable()
                ->constrained('chat_messages')
                ->nullOnDelete()
                ->comment('Message this is replying to');

            // Metadata (JSON for flexible storage)
            $table->json('metadata')->nullable()->comment('Additional message metadata');

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

            // Indexes for performance
            $table->index('conversation_id');
            $table->index('sender_type');
            $table->index(['conversation_id', 'created_at']);
            $table->index('created_at');
        });
    }

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