<?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('reviews', function (Blueprint $table) {
            $table->id();
            $table->foreignId('product_id')->constrained('products')->cascadeOnDelete();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->tinyInteger('rating')->unsigned(); // 1 to 5 stars
            $table->text('comment')->nullable();
            $table->timestamps();
            
            // A user can only leave one review per product
            $table->unique(['product_id', 'user_id']); 
        });
    }

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