<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    public function up(): void
    {
        Schema::create('messages', function (Blueprint $table) {
            $table->string('id')->primary();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->text('message');
            $table->string('room_type')->default('general'); // general, ticket-{id}, etc.
            $table->string('room_id')->nullable(); // for ticket-specific rooms
            $table->timestamps();
        });

        Schema::table('messages', function (Blueprint $table) {
            $table->index(['room_type', 'room_id']);
            $table->index('created_at');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('messages');
    }
};