<?php

declare(strict_types=1);

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('post_comments', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->foreignUuid('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignUuid('post_id')->constrained('posts')->cascadeOnDelete();
            $table->uuid('parent_id')->nullable();
            $table->foreignUuid('user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->string('guest_name', 60)->nullable();
            $table->string('guest_password_hash', 255)->nullable();

            $table->text('content');
            $table->boolean('is_secret')->default(false);
            $table->unsignedInteger('like_count')->default(0);
            $table->string('ip', 45)->nullable();

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

            $table->index(['tenant_id', 'post_id', 'created_at']);
            $table->index('parent_id');
            $table->foreign('parent_id')->references('id')->on('post_comments')->cascadeOnDelete();
        });
    }

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