<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * Model: RReviewFeedback (Research Review Feedback)
 * 
 * Stores peer review comments and decisions for research publications.
 * Supports the complete review workflow with standard criteria evaluation.
 * 
 * @property int $id
 * @property int $publication_id
 * @property int $reviewer_id
 * @property string $decision
 * @property string $comments
 * @property array|null $criteria_evaluation
 * @property \Carbon\Carbon|null $reviewed_at
 */
class RReviewFeedback extends Model
{
    use HasFactory;

    protected $table = 'r_review_feedbacks';

    protected $fillable = [
        'publication_id',
        'reviewer_id',
        'decision',
        'comments',
        'criteria_evaluation',
        'reviewed_at',
    ];

    protected $casts = [
        'criteria_evaluation' => 'array',
        'reviewed_at' => 'datetime',
    ];

    /**
     * Decision constants.
     */
    const DECISION_APPROVE = 'approve';
    const DECISION_REQUEST_REVISION = 'request_revision';
    const DECISION_REJECT = 'reject';

    /**
     * Standard review criteria.
     */
    const CRITERIA = [
        'originality' => 'Originality and Novelty',
        'methodology' => 'Methodology Soundness',
        'significance' => 'Significance and Impact',
        'clarity' => 'Clarity and Presentation',
        'references' => 'References Quality',
        'ethics' => 'Ethical Considerations',
    ];

    /**
     * Get the publication being reviewed.
     */
    public function publication(): BelongsTo
    {
        return $this->belongsTo(RPublication::class, 'publication_id');
    }

    /**
     * Get the reviewer.
     */
    public function reviewer(): BelongsTo
    {
        return $this->belongsTo(User::class, 'reviewer_id');
    }

    /**
     * Check if decision is approval.
     */
    public function isApproved(): bool
    {
        return $this->decision === self::DECISION_APPROVE;
    }

    /**
     * Check if revision is requested.
     */
    public function isRevisionRequested(): bool
    {
        return $this->decision === self::DECISION_REQUEST_REVISION;
    }

    /**
     * Check if rejected.
     */
    public function isRejected(): bool
    {
        return $this->decision === self::DECISION_REJECT;
    }

    /**
     * Get decision label.
     */
    public function getDecisionLabelAttribute(): string
    {
        return match($this->decision) {
            self::DECISION_APPROVE => 'Approved',
            self::DECISION_REQUEST_REVISION => 'Revision Requested',
            self::DECISION_REJECT => 'Rejected',
            default => ucfirst($this->decision),
        };
    }

    /**
     * Get decision color for UI.
     */
    public function getDecisionColorAttribute(): string
    {
        return match($this->decision) {
            self::DECISION_APPROVE => 'green',
            self::DECISION_REQUEST_REVISION => 'yellow',
            self::DECISION_REJECT => 'red',
            default => 'gray',
        };
    }

    /**
     * Get average score from criteria evaluation.
     */
    public function getAverageScoreAttribute(): ?float
    {
        if (!$this->criteria_evaluation) {
            return null;
        }

        $scores = array_filter($this->criteria_evaluation, 'is_numeric');
        
        if (empty($scores)) {
            return null;
        }

        return round(array_sum($scores) / count($scores), 2);
    }

    /**
     * Scope: Reviews by a specific reviewer.
     */
    public function scopeByReviewer($query, int $reviewerId)
    {
        return $query->where('reviewer_id', $reviewerId);
    }

    /**
     * Scope: Reviews for a specific publication.
     */
    public function scopeForPublication($query, int $publicationId)
    {
        return $query->where('publication_id', $publicationId);
    }

    /**
     * Scope: Approved reviews.
     */
    public function scopeApproved($query)
    {
        return $query->where('decision', self::DECISION_APPROVE);
    }

    /**
     * Scope: Rejected reviews.
     */
    public function scopeRejected($query)
    {
        return $query->where('decision', self::DECISION_REJECT);
    }
}
