<?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('avaliacoes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->onDelete('cascade');
            $table->string('avaliavel_type');
            $table->unsignedBigInteger('avaliavel_id');
            $table->tinyInteger('nota')->unsigned();
            $table->text('comentario')->nullable();
            $table->timestamps();

            $table->unique(['user_id', 'avaliavel_type', 'avaliavel_id'], 'unique_avaliacao_por_user_local');
            $table->index(['avaliavel_type', 'avaliavel_id']);
        });
    }

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