<?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 ticket_replies table for ticket thread management.
     * 
     * Requirements: 1.4, 6.5, 8.1
     */
    public function up(): void
    {
        Schema::create('ticket_replies', function (Blueprint $table) {
            // Primary key
            $table->id();

            // Foreign key to tickets table
            $table->foreignId('ticket_id')
                ->constrained()
                ->onDelete('cascade')
                ->comment('The ticket this reply belongs to');

            // Foreign key to users table (who wrote the reply)
            $table->foreignId('user_id')
                ->constrained()
                ->onDelete('cascade')
                ->comment('The user who wrote this reply');

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

            // Internal note flag (for staff only notes)
            $table->boolean('is_internal')
                ->default(false)
                ->comment('Whether this is an internal note not visible to ticket owner');

            // Optional attachments (stored as JSON array of file paths)
            $table->json('attachments')->nullable()->comment('Array of attachment file paths');

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

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

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