<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('contact_messages', function (Blueprint $table) {
            $table->id();
            $table->foreignId('academy_id')->constrained('academies');
            $table->foreignId('user_id')->constrained('users');
            $table->string('participant_uuid', 36)->nullable();
            $table->string('subject', 200);
            $table->text('body');
            $table->string('source', 30)->default('mobile_app');
            $table->text('reply')->nullable();
            $table->foreignId('replied_by')->nullable()->constrained('users');
            $table->timestamp('replied_at')->nullable();
            $table->boolean('is_read')->default(false);
            $table->timestamps();

            $table->index(['academy_id', 'is_read']);
            $table->index(['user_id', 'created_at']);
        });

        DB::statement("ALTER TABLE contact_messages ADD CONSTRAINT contact_messages_source_check CHECK (source IN ('mobile_app', 'web', 'admin'))");
    }

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