<?php

namespace App\Http\Requests\Research;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use App\Models\RReviewFeedback;

/**
 * Form Request: ReviewPublicationRequest
 * 
 * Validates review feedback submission data.
 * 
 * @author SSHIA Development Team
 */
class ReviewPublicationRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        // Check if user has permission to review
        return auth()->check() && auth()->user()->can('review publications');
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'decision' => [
                'required',
                Rule::in([
                    RReviewFeedback::DECISION_APPROVE,
                    RReviewFeedback::DECISION_REQUEST_REVISION,
                    RReviewFeedback::DECISION_REJECT,
                ]),
            ],
            'comments' => [
                'required',
                'string',
                'min:50',
                'max:5000',
            ],
            'criteria_evaluation' => [
                'sometimes',
                'array',
            ],
            'criteria_evaluation.originality' => [
                'sometimes',
                'integer',
                'min:1',
                'max:5',
            ],
            'criteria_evaluation.methodology' => [
                'sometimes',
                'integer',
                'min:1',
                'max:5',
            ],
            'criteria_evaluation.significance' => [
                'sometimes',
                'integer',
                'min:1',
                'max:5',
            ],
            'criteria_evaluation.clarity' => [
                'sometimes',
                'integer',
                'min:1',
                'max:5',
            ],
            'criteria_evaluation.references' => [
                'sometimes',
                'integer',
                'min:1',
                'max:5',
            ],
            'criteria_evaluation.ethics' => [
                'sometimes',
                'integer',
                'min:1',
                'max:5',
            ],
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'decision.required' => 'Please select a review decision.',
            'decision.in' => 'Please select a valid decision.',
            'comments.required' => 'Review comments are required.',
            'comments.min' => 'Comments must be at least 50 characters.',
            'comments.max' => 'Comments cannot exceed 5000 characters.',
            'criteria_evaluation.*.integer' => 'Each criterion must be rated between 1 and 5.',
            'criteria_evaluation.*.min' => 'Each criterion must be rated at least 1.',
            'criteria_evaluation.*.max' => 'Each criterion cannot exceed 5.',
        ];
    }

    /**
     * Configure the validator instance.
     */
    public function withValidator($validator): void
    {
        $validator->after(function ($validator) {
            // Require criteria evaluation for revision requests
            if ($this->decision === RReviewFeedback::DECISION_REQUEST_REVISION) {
                if (empty($this->criteria_evaluation)) {
                    $validator->errors()->add(
                        'criteria_evaluation',
                        'Criteria evaluation is required when requesting revisions.'
                    );
                }
            }
        });
    }

    /**
     * Get validated data with additional fields.
     */
    public function validated($key = null, $default = null): array
    {
        $validated = parent::validated($key, $default);

        // Add reviewer ID
        $validated['reviewer_id'] = auth()->id();
        $validated['reviewed_at'] = now();

        return $validated;
    }
}
