<?php

namespace App\Http\Requests\Checklist;

use Illuminate\Foundation\Http\FormRequest;

class UpdateEntryRequest extends FormRequest
{
    public function authorize(): bool
    {
        return ! $this->user()->isFrozen();
    }

    protected function prepareForValidation(): void
    {
        if ($this->filled('entry_time')) {
            $time = (string) $this->input('entry_time');
            if (preg_match('/^\d{1,2}:\d{2}$/', $time)) {
                $this->merge(['entry_time' => $time . ':00']);
            }
        }
    }

    public function rules(): array
    {
        return [
            'entry_date'           => ['sometimes', 'date_format:Y-m-d'],
            'entry_time'           => ['sometimes', 'date_format:H:i:s'],
            'condition_status'     => ['sometimes', 'in:good,warning,critical'],
            'issue_found'          => ['sometimes', 'boolean'],
            'maintenance_required' => ['sometimes', 'boolean'],
            'custom_fields'        => ['nullable', 'array'],
            'notes'                => ['nullable', 'string'],
            'image'                => ['nullable', 'image', 'max:5120'],
        ];
    }
}
