<?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
{
if (! Schema::hasTable('reviews')) Schema::create('reviews', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->foreignId('product_id')->constrained()->cascadeOnDelete();
$table->foreignId('order_id')->nullable()->constrained()->nullOnDelete();
$table->foreignId('order_item_id')->nullable()->constrained('order_items')->nullOnDelete();
$table->unsignedTinyInteger('rating'); // 1-5
$table->string('title')->nullable();
$table->text('body')->nullable();
$table->json('media')->nullable(); // uploaded review photos
$table->boolean('is_verified_purchase')->default(false);
$table->text('vendor_reply')->nullable();
$table->timestamp('vendor_replied_at')->nullable();
$table->enum('status', ['pending', 'approved', 'rejected'])->default('pending');
$table->string('rejection_reason')->nullable();
$table->unsignedInteger('helpful_count')->default(0);
$table->timestamps();
$table->unique(['user_id', 'product_id', 'order_item_id']);
$table->index(['product_id', 'status', 'rating']);
$table->index(['user_id', 'status']);
});
}
public function down(): void
{
Schema::dropIfExists('reviews');
}
};