<?php

namespace App\Http\Requests\Admin\AcademicClasses;

use App\Enums\SubmissionStatus;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class SubmissionRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()->can($this->isMethod('post') ? 'create-assignment-submissions' : 'update-assignment-submissions');
    }

    public function rules(): array
    {
        $assignment = $this->route('assignment');
        $submission = $this->route('submission');

        $rules = [
            'notes' => ['nullable', 'string'],
            'status' => ['required', Rule::enum(SubmissionStatus::class)],
            'submitted_at' => ['nullable', 'date'],
            'score' => ['nullable', 'numeric', 'min:0', 'max:100'],
            'lecturer_feedback' => ['nullable', 'string'],
            'file' => ['nullable', 'file', 'max:10240', 'mimes:pdf,doc,docx,ppt,pptx,xls,xlsx,jpg,jpeg,png,mp4,zip'],
        ];

        if (! $submission) {
            $rules['student_id'] = [
                'required',
                'integer',
                Rule::exists('class_enrollments', 'student_id')->where('course_class_id', $assignment->course_class_id),
                Rule::unique('submissions', 'student_id')->where('assignment_id', $assignment->id),
            ];
        }

        return $rules;
    }
}
