<?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_submissions', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('academy_id')->constrained('academies');
            $table->string('name');
            $table->string('phone', 20);
            $table->string('email')->nullable();
            $table->text('message');
            $table->string('status', 20)->default('new');
            $table->text('admin_notes')->nullable();
            $table->foreignId('replied_by')->nullable()->constrained('users');
            $table->timestamp('read_at')->nullable();
            $table->timestamp('replied_at')->nullable();
            $table->timestamps();

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

        DB::statement("ALTER TABLE contact_submissions ADD CONSTRAINT contact_submissions_status_check
            CHECK (status IN ('new', 'read', 'replied', 'archived'))");
    }

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