<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UserRequest extends FormRequest
{
   
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        $userID = !empty(request()->id) ? decrypt_id(request()->id) : null;
        return [
            'name'             => 'required',
            'gender'           => 'required',
            'role_id'          => 'required',
            'email'            => 'required|string|regex:/(^[a-zA-Z]+[a-zA-Z0-9\\-@.]*$)/u|unique:users,email,' . $userID,
            'password'         => $userID ? 'nullable' : 'required|same:confirm_password',
            'confirm_password' => $userID ? 'nullable' : 'required|same:password',

            'type'                    => 'required|in:1,2,3',
            // 'user_details'            => 'required|array|min:1',
            // 'user_details.*.id'       => 'required|integer|exists:branches,id',
            // 'user_details.*.selected' => 'required|boolean',
        ];
    }

    // public function withValidator($validator)
    // {
    //     $validator->after(function ($validator) {
    //         $userDetails = $this->input('user_details', []);


    //         $selected = collect($userDetails)->contains(function ($detail) {
    //             return isset($detail['selected']) && $detail['selected'] === true;
    //         });

    //         if (!$selected) {
    //             $validator->errors()->add('user_details', 'You must select at ប្រភេទ and សាខា.');
    //         }
    //     });
    // }

    // public function rules()
    // {
    //     return  [
    //         'name'     => 'required',
    //         'gender'   => 'required',
    //         'type' => 'required|in:1,2,3',
    //         'role_id'  => 'required',
    //         'email'    => 'required|string|regex:/(^[a-zA-Z]+[a-zA-Z0-9\\-@.]*$)/u|unique:users,email,' . request()->id,
    //         'password' => isset(request()->id) ? 'nullable' : 'required|same:confirm_password',
    //         'confirm_password'  => isset(request()->id) ? 'nullable' : 'required|same:password',
    //     ];
    // }
}
