<?php

namespace App\Http\Requests\V3\RoadPatrolProject;

use App\Rules\VerificationCode;
use Illuminate\Foundation\Http\FormRequest;

class StoreRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'verification_code' => ['required', 'string', new VerificationCode($this->phone_number)],
            'distance' => 'numeric',
            'start_time' => 'required|date_format:Y-m-d H:i',
            'end_time' => 'required|date_format:Y-m-d H:i|after:start_time',
            'fuel_consumption' => 'required|numeric',
            'vehicle_runtime' => 'required|integer',
            'stop_points' => 'required|array',
            'road_patrol_machines_id' => 'required|array',
            'road_patrol_machines_id.*' => 'required|exists:cmms_machines,id',
            'road_patrol_rahdaran_id' => 'required|array',
            'road_patrol_rahdaran_id.*' => 'required|exists:rahdaran,id',

            'observed_items' => 'array',
            'observed_items.*.item_id' => 'required|integer',
            'observed_items.*.sub_item_id' => 'required|integer',
            'observed_items.*.description' => '',
            'observed_items.*.instant_action' => 'required|in:0,1',
            'observed_items.*.local_name' => 'required_if:observed_items.*.instant_action,0',

            // road item projects fields
            'observed_items.*.start_point' => 'required',
            'observed_items.*.end_point' => '',
            'observed_items.*.amount' => 'numeric|required_if:observed_items.*.instant_action,1',
            'observed_items.*.before_image' => 'image|max:4096',
            'observed_items.*.after_image' => 'image|max:4096',
            'observed_items.*.priority' => 'integer|in:1,2,3|required_if:observed_items.*.instant_action,0',
            'description' => 'string',

            'observed_items.*.road_item_machines_id' => 'required_if:observed_items.*.instant_action,1|array',
            'observed_items.*.road_item_machines_id.*' => 'required_if:observed_items.*.instant_action,1|exists:cmms_machines,id',
            'observed_items.*.road_item_rahdaran_id' => 'required_if:observed_items.*.instant_action,1|array',
            'observed_items.*.road_item_rahdaran_id.*' => 'required_if:observed_items.*.instant_action,1|exists:rahdaran,id',
        ];
    }

    public function attributes(): array
    {
        return [
            'start_time' => 'ساعت شروع گشت',
            'end_time' => 'ساعت پایان گشت',
        ];
    }
}
