<?php

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

/**
 * Migration: Research and Innovation Centre - Review Feedbacks Table
 * 
 * Stores peer review comments, decisions, and feedback from managers and mentors
 * on research publications. Supports the complete review workflow.
 * 
 * @author SSHIA Development Team
 * @version 1.0.0
 */
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('r_review_feedbacks', function (Blueprint $table) {
            // Primary Key
            $table->id();
            
            // Foreign Keys
            $table->foreignId('publication_id')
                  ->constrained('r_publications')
                  ->onDelete('cascade')
                  ->comment('The publication being reviewed');
            
            $table->foreignId('reviewer_id')
                  ->constrained('users')
                  ->onDelete('cascade')
                  ->comment('The user providing the review');
            
            // Review Decision
            $table->enum('decision', ['approve', 'request_revision', 'reject'])
                  ->comment('Review decision');
            
            // Review Content
            $table->text('comments')
                  ->comment('Detailed review comments and feedback');
            
            // Standard Criteria Evaluation (JSON for flexibility)
            $table->json('criteria_evaluation')
                  ->nullable()
                  ->comment('Evaluation against standard criteria');
            
            // Timestamps
            $table->timestamp('reviewed_at')
                  ->nullable()
                  ->comment('When the review was completed');
            
            $table->timestamps();
            
            // Indexes
            $table->index('publication_id');
            $table->index('reviewer_id');
            $table->index('decision');
            $table->index('reviewed_at');
            
            // Unique constraint to prevent duplicate reviews
            $table->unique(['publication_id', 'reviewer_id'], 'r_review_feedbacks_unique_review');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('r_review_feedbacks');
    }
};
