<?php

namespace App\Http\Requests\Admin\AcademicClasses\Assignment;

use App\Enums\AssignmentStatus;
use App\Enums\UserRole;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class AssignmentRequest extends FormRequest
{
    public function authorize(): bool
    {
        if ($this->isMethod('post')) {
            return $this->user()->can('create-assignments');
        }

        return $this->user()->can('update-assignments')
            && $this->user()->canManageAssignment($this->route('assignment'));
    }

    public function rules(): array
    {
        $user = $this->user();

        $courseClassRule = Rule::exists('course_classes', 'id');

        if ($user->hasRole(UserRole::Dosen->value)) {
            $courseClassRule->where('lecturer_id', $user->lecturer?->id);
        }

        return [
            'course_class_id' => ['required', 'integer', $courseClassRule],
            'title' => ['required', 'string', 'max:150'],
            'description' => ['nullable', 'string'],
            'deadline' => ['required', 'date'],
            'status' => ['nullable', Rule::enum(AssignmentStatus::class)],
            'attachment' => ['nullable', 'file', 'max:10240', 'mimes:pdf,doc,docx,ppt,pptx,xls,xlsx,jpg,jpeg,png,mp4,zip'],
        ];
    }
}
