<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * ADD-01 from the system analysis: no rating/review system existed anywhere
 * despite full in-app chat already being built between customer and driver.
 * Generic rater/ratee morphs (rather than two fixed FK pairs) so the same
 * table covers both directions — a customer rating their driver, and a
 * driver rating their passenger — after a completed trip.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('ratings', function (Blueprint $table) {
            $table->id();
            $table->foreignId('booking_id')->constrained()->cascadeOnDelete();
            $table->morphs('rater');
            $table->morphs('ratee');
            $table->unsignedTinyInteger('stars');
            $table->string('comment', 500)->nullable();
            $table->timestamps();

            // morphs() above already indexes (ratee_type, ratee_id) and
            // (rater_type, rater_id) by default — only the extra
            // booking-scoped uniqueness constraint is needed here.
            $table->unique(['booking_id', 'rater_type', 'rater_id'], 'ratings_one_per_rater_per_booking');
        });
    }

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