<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Models\User;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class UpdateUserRequest 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
    {
//  $userRole = User::find($this->route('user'));
//         $userRole->assignRole('learner');
        $userRole = User::find($this->route('user'))->getRoleNames()[0];
        if ($userRole == 'learner') {

            return [
                'phone'            => ['nullable', 'string', 'regex:/^\+?[0-9\s\-]{7,20}$/'],
                'gender'           => ['nullable', 'in:male,female,other,prefer not to say'],
                'dob'              => ['nullable', 'date', 'before:today'],
                'country'          => ['nullable', 'string', 'max:100'],
                'state'            => ['nullable', 'string', 'max:100'],
                'city'             => ['nullable', 'string', 'max:100'],
                'address'          => ['nullable', 'string', 'max:255'],
                'education_level'  => ['nullable', 'string', 'in:High School,Associate,Bachelor\'s,Master\'s,Doctorate'],
                'bio'              => ['nullable', 'string', 'max:1000'],
                'avatar'           => ['nullable'],
                'interests'        => ['nullable', 'array'],
                'interests.*'      => ['string', 'max:100'],
                'email' => 'required|email|unique:users,email,' . $this->route('user'),
                'first_name'          => ['nullable', 'string', 'max:100'],
                'last_name'          => ['nullable', 'string', 'max:100'],
                'password'          => ['nullable', 'string', 'min:8'],
                'role'          => ['nullable', 'string', 'in:admin,manager_general,sub_manager,teacher,learner,director,manager']
                
            ];
        } elseif ($userRole == 'teacher') {
            return [
                'phone'            => ['nullable', 'string', 'regex:/^\+?[0-9\s\-]{7,20}$/'],
                'gender'           => ['nullable', 'in:male,female,other,prefer not to say'],
                'dob'              => ['nullable', 'date', 'before:today'],
                'country'          => ['nullable', 'string', 'max:100'],
                'state'            => ['nullable', 'string', 'max:100'],
                'city'             => ['nullable', 'string', 'max:100'],
                'address'          => ['nullable', 'string', 'max:255'],
                'highest_qualification'  => ['nullable', 'string', 'in:High School,Associate,Bachelor\'s,Master\'s,Doctorate'],
                'bio'              => ['nullable', 'string', 'max:1000'],
                'avatar'           => ['nullable'],
                'specialization'   => ['nullable'],
                'years_of_experience'   => ['nullable'],
                'subjects'   => ['nullable'],
                'availability'   => ['nullable'],
                'social_links'   => ['nullable'],
                'email' => 'required|email|unique:users,email,' . $this->route('user'),
                'first_name'          => ['nullable', 'string', 'max:100'],
                'last_name'          => ['nullable', 'string', 'max:100'],
                                'password'          => ['nullable', 'string', 'min:8'],
                'role'          => ['nullable', 'string', 'in:admin,manager_general,sub_manager,teacher,learner,director,manager']


            ];
        } elseif ($userRole == 'manager') {
            return [
                'phone'            => ['nullable', 'string', 'regex:/^\+?[0-9\s\-]{7,20}$/'],
                'gender'           => ['nullable', 'in:male,female,other,prefer not to say'],
                'dob'              => ['nullable', 'date', 'before:today'],
                'country'          => ['nullable', 'string', 'max:100'],
                'state'            => ['nullable', 'string', 'max:100'],
                'city'             => ['nullable', 'string', 'max:100'],
                'address'          => ['nullable', 'string', 'max:255'],
                'highest_qualification'  => ['nullable', 'string', 'in:High School,Associate,Bachelor\'s,Master\'s,Doctorate'],
                'bio'              => ['nullable', 'string', 'max:1000'],   
                'avatar'           => ['nullable'],
                'specialization'   => ['nullable'],
                'years_of_experience'   => ['nullable'],
                'subjects'   => ['nullable'],
                'availability'   => ['nullable'],
                'social_links'   => ['nullable'],
                'email' => 'required|email|unique:users,email,' . $this->route('user'),
                'first_name'          => ['nullable', 'string', 'max:100'],
                'last_name'          => ['nullable', 'string', 'max:100'],
                'password'          => ['nullable', 'string', 'min:8'],
                'role'          => ['nullable', 'string', 'in:admin,manager_general,sub_manager,teacher,learner,director,manager'],
                                'user'           =>         ['nullable','array']


            ];
        }
        elseif ($userRole == 'manager_general' || $userRole == 'sub_manager'|| $userRole == 'director') {
            return [
 
                'avatar'           => ['nullable'],          
                'email' => 'required|email|unique:users,email,' . $this->route('user'),
                'first_name'          => ['nullable', 'string', 'max:100'],
                'last_name'          => ['nullable', 'string', 'max:100'],
                'password'          => ['nullable', 'string', 'min:8'],
                'role'          => ['nullable', 'string', 'in:admin,manager_general,sub_manager,teacher,learner,director,manager'],
                'bio'              => ['nullable', 'string', 'max:1000'],
                'user'           =>         ['nullable','array']

                ];
        }
    }

    public function messages()
    {
        $userRole = \App\Models\User::find($this->route('user'))->getRoleNames()[0];

        $messages = [
            'phone.regex' => __('validation.profile.phone_invalid'),
            'gender.in' => __('validation.profile.gender_invalid'),
            'dob.date' => __('validation.profile.dob_date'),
            'dob.before' => __('validation.profile.dob_before'),
            'country.string' => __('validation.profile.country_string'),
            'state.string' => __('validation.profile.state_string'),
            'city.string' => __('validation.profile.city_string'),
            'address.string' => __('validation.profile.address_string'),

            'bio.string' => __('validation.profile.bio_string'),
            'bio.max' => __('validation.profile.bio_max'),

            'email.required' => __('validation.profile.email_required'),
            'email.email' => __('validation.profile.email_invalid'),
            'email.unique' => __('validation.profile.email_unique'),

            'first_name.string' => __('validation.profile.first_name_string'),
            'first_name.max' => __('validation.profile.first_name_max'),
            'last_name.string' => __('validation.profile.last_name_string'),
            'last_name.max' => __('validation.profile.last_name_max'),
            'password.min' => 'Le mot de passe doit contenir au moins 8 caractères.',

        ];

        if ($userRole === 'learner') {
            $messages = array_merge($messages, [
                'education_level.string' => __('validation.learner.education_level.string'),

                'interests.array' => __('validation.learner.interests.array'),
                'interests.*.string' => __('validation.learner.interests.string'),
                'interests.*.max' => __('validation.learner.interests.max'),
            ]);
        }

        if ($userRole === 'teacher') {
            $messages = array_merge($messages, [
                'highest_qualification.string' => __('validation.teacher.highest_qualification.string'),

                'specialization.string' => __('validation.teacher.specialization.string'),
                'years_of_experience.numeric' => __('validation.teacher.years_of_experience.numeric'),

                'subjects.array' => __('validation.teacher.subjects.array'),
                'subjects.*.string' => __('validation.teacher.subjects.string'),

                'availability.string' => __('validation.teacher.availability.string'),
                'social_links.string' => __('validation.teacher.social_links.string'),
            ]);
        }

        if ($userRole === 'manager') {
            $messages = array_merge($messages, [
                'subjects.array' => __('validation.manager.subjects.array'),
            ]);
        }

        return $messages;
    }


    // public function failedValidation(Validator $validator)
    // {
    //     throw new HttpResponseException(response()->json([
    //         'status' => false,
    //         'message' => 'Validation failed',
    //         'errors' => $validator->errors()
    //     ], 422));
    // }
}