<?php

namespace App\Http\Requests\Admin\AcademicClasses;

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

class AttendanceStoreRequest extends FormRequest
{
    public function authorize(): bool
    {
        if (! $this->user()->can('create-attendances')) {
            return false;
        }

        $user = $this->user();
        $courseClass = $this->route('course_class');

        if ($user->hasRole('dosen') && $courseClass?->lecturer_id !== $user->lecturer?->id) {
            return false;
        }

        return true;
    }

    public function rules(): array
    {
        return [
            'date' => ['required', 'date'],
            'records' => ['required', 'array', 'min:1'],
            'records.*.student_id' => ['required', 'integer', Rule::exists('students', 'id')],
            'records.*.status' => ['required', Rule::enum(AttendanceStatus::class)],
        ];
    }
}
