<?php

namespace App\Http\Requests\ClassProgress;

use App\Http\Requests\ClassProgress\ClassProgressFormRequest;

class ClassProgressUpdateRequest extends ClassProgressFormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        $statuses = array_keys((array) config('progress.status_options', []));
        $maxFileKb = (int) config('progress.attachments.max_kb', 5120);
        $mimes = (array) config('progress.attachments.mimes', ['pdf', 'jpg', 'jpeg', 'png']);
        $mimeRule = 'mimes:' . implode(',', $mimes);

        return [
            'week_start' => ['nullable', 'date_format:Y-m-d'],
            'week_end' => ['nullable', 'date_format:Y-m-d'],
            'covered' => ['nullable', 'string'],
            'homework' => ['nullable', 'string'],
            'unit_title' => ['nullable', 'string', 'max:120'],
            'status' => $statuses ? ['nullable', 'in:' . implode(',', $statuses)] : ['nullable', 'string'],
            'support_needed' => ['nullable', 'string'],
            'flags' => ['nullable', 'array'],
            'attachments' => ['nullable', 'array'],
            'attachments.*' => ['nullable', 'file', $mimeRule, 'max:' . $maxFileKb],
        ];
    }

    public function attachments(): array
    {
        return $this->file('attachments') ?? [];
    }
}
