<?php

use App\Enums\ModerationCaseStatus;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('moderation_cases', function (Blueprint $table) {
            $table->ulid('id')->primary();
            $table->ulidMorphs('caseable');
            $table->string('status', 32)->default(ModerationCaseStatus::OPEN->value);
            $table->unsignedInteger('reports_count')->default(0);
            $table->unsignedSmallInteger('threshold')->default(3);
            $table->timestamp('threshold_notified_at')->nullable();
            $table->foreignUlid('assigned_to')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignUlid('resolved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('resolved_at')->nullable();
            $table->text('resolution_note')->nullable();
            $table->timestamps();

            $table->unique(['caseable_type', 'caseable_id'], 'moderation_cases_caseable_unique');
            $table->index(['status', 'created_at']);
        });
    }

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