<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('reviews', function (Blueprint $table) {
            $table->id();
            $table->foreignId('ride_id')
                  ->constrained()
                  ->onDelete('cascade');
            $table->foreignId('user_id')
                  ->constrained()
                  ->onDelete('cascade');
            $table->integer('rating')
                  ->unsigned()
                  ->comment('Rating from 1 to 5');
            $table->string('title', 100)
                  ->nullable();
            $table->text('content');
            $table->string('status')
                  ->default(ReviewStatus::PENDING->value);
            $table->timestamp('moderated_at')
                  ->nullable();
            $table->foreignId('moderated_by')
                  ->nullable()
                  ->constrained('users')
                  ->nullOnDelete();
            $table->integer('helpful_votes_count')
                  ->default(0)
                  ->unsigned();
            $table->timestamps();

            // Ensure one review per ride per user
            $table->unique(['ride_id', 'user_id']);

            // Indexes for common queries
            $table->index(['ride_id', 'status']);
            $table->index(['user_id', 'created_at']);
            $table->index('status');
        });
    }

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