<?php

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('comments', function (Blueprint $table) {
            $table->id();

            // Polymorphic relation to commentable model (Page, Product, etc.)
            $table->string('commentable_type');
            $table->unsignedBigInteger('commentable_id');

            // Parent comment for nested replies
            $table->unsignedBigInteger('parent_id')->nullable();

            // User (if logged in)
            $table->unsignedBigInteger('user_id')->nullable();

            // Guest info (if not logged in)
            $table->string('guest_name')->nullable();
            $table->string('guest_email')->nullable();

            // Content
            $table->text('content');

            // Rating (1-5 stars)
            $table->tinyInteger('rating')->nullable();

            // Status
            $table->enum('status', ['pending', 'approved', 'rejected', 'spam'])->default('pending');
            $table->boolean('is_approved')->default(false);
            $table->timestamp('approved_at')->nullable();
            $table->unsignedBigInteger('approved_by')->nullable();

            // Tracking
            $table->string('ip_address', 45)->nullable();
            $table->text('user_agent')->nullable();

            // Extra data
            $table->json('metadata')->nullable();

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

            // Indexes
            $table->index(['commentable_type', 'commentable_id']);
            $table->index('parent_id');
            $table->index('user_id');
            $table->index('status');
            $table->index('is_approved');
            $table->index('created_at');

            // Foreign keys
            $table->foreign('parent_id')->references('id')->on('comments')->onDelete('cascade');
        });
    }

    public function down(): void
    {

    }
};
