<?php

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
    {
        if (!Schema::hasTable('forum_posts')) {
            Schema::create('forum_posts', function (Blueprint $table) {
                $table->id();

                // Added in a follow-up migration because forum_threads is created
                // later in filename order even though it shares the same timestamp.
                $table->unsignedBigInteger('thread_id');
                $table->foreignId('user_id')->constrained()->cascadeOnDelete();

                $table->longText('content');

                $table->boolean('is_edited')->default(false);
                $table->timestamp('edited_at')->nullable();

                $table->timestamps();
                $table->softDeletes();

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

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