<?php

namespace App\Http\Requests\User;

use Illuminate\Validation\Rule;
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 [
            "id_prefix" => ["required", "string"],

            "id_no" => [
                "required",
                "numeric",
                Rule::unique("users")
                    ->where(
                        fn($query) => $query->where(
                            "id_prefix",
                            $this->id_prefix
                        )
                    )
                    ->ignore($this->route("user")),
            ],

            "first_name" => ["required"],

            "middle_name" => ["string", "nullable"],

            "last_name" => ["required"],

            "suffix" => ["string", "nullable"],

            "position_name" => ["required", "string", "max:255"],

            "one_charging_sync_id" => [
                "exists:one_charging,sync_id,deleted_at,NULL",
            ],

            "warehouse_id" => [
                "required",
                "exists:warehouses,id,deleted_at,NULL",
                "numeric",
            ],

            "farm_id" => [
                "nullable",
                "integer",
                "exists:farms,id,deleted_at,NULL",
            ],

            "username" => [
                "required",
                Rule::unique("users")->ignore($this->route("user")),
            ],

            "role_id" => [
                "required",
                "exists:roles,id,deleted_at,NULL",
                "numeric",
            ],
        ];
    }
}
