<?php

namespace App\Http\Requests;


use OpenApi\Attributes as OA;
use App\Models\User;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Validation\Rules\Password;

#[OA\Schema(
    schema: 'UpdateUserRequest',
    required: ['phone', 'gender', 'dob', 'country', 'state', 'city', 'address', 'education_level', 'bio', 'avatar', 'interests', 'email', 'first_name', 'last_name', 'password', 'role', 'highest_qualification', 'specialization', 'years_of_experience', 'subjects', 'availability', 'social_links', 'user', 'status', 'message', 'errors'],
    properties: [
        new OA\Property(property: 'phone', type: 'string'),
        new OA\Property(property: 'gender', type: 'string'),
        new OA\Property(property: 'dob', type: 'string'),
        new OA\Property(property: 'country', type: 'string'),
        new OA\Property(property: 'state', type: 'string'),
        new OA\Property(property: 'city', type: 'string'),
        new OA\Property(property: 'address', type: 'string'),
        new OA\Property(property: 'education_level', type: 'string'),
        new OA\Property(property: 'bio', type: 'string'),
        new OA\Property(property: 'avatar', type: 'string'),
        new OA\Property(property: 'interests', type: 'string'),
        new OA\Property(property: 'email', type: 'string', format: 'email'),
        new OA\Property(property: 'first_name', type: 'string'),
        new OA\Property(property: 'last_name', type: 'string'),
        new OA\Property(property: 'password', type: 'string', format: 'password'),
        new OA\Property(property: 'role', type: 'string'),
        new OA\Property(property: 'highest_qualification', type: 'string'),
        new OA\Property(property: 'specialization', type: 'string'),
        new OA\Property(property: 'years_of_experience', type: 'string'),
        new OA\Property(property: 'subjects', type: 'string'),
        new OA\Property(property: 'availability', type: 'string'),
        new OA\Property(property: 'social_links', type: 'string'),
        new OA\Property(property: 'user', type: 'string'),
        new OA\Property(property: 'status', type: 'string'),
        new OA\Property(property: 'message', type: 'string'),
        new OA\Property(property: 'errors', type: 'string')
    ]
)]
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, 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',  Password::default()],
                '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',  Password::default()],
                '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',  Password::default()],
                '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',  Password::default()],
                'role' => ['nullable', 'string', 'in:admin,manager_general,sub_manager,teacher,learner,director,manager'],
                'bio' => ['nullable', 'string', 'max:1000'],
                'user' => ['nullable', 'array'],

            ];
        } elseif ($userRole == 'admin') {
            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',  Password::default()],
                'role' => ['nullable', 'string', 'in:admin,manager_general,sub_manager,teacher,learner,director,manager'],
                'bio' => ['nullable', 'string', 'max:1000'],

            ];
        }
    }

    public function messages()
    {
        $userRole = 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));
    // }
}
