<?php

namespace App\Http\Requests\Checklist;

use Illuminate\Foundation\Http\FormRequest;

class StoreEntryRequest 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');
            // Accept H:i from mobile clients and normalize to H:i:s
            if (preg_match('/^\d{1,2}:\d{2}$/', $time)) {
                $this->merge(['entry_time' => $time . ':00']);
            }
        }
    }

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