<?php

namespace App\Http\Requests;

use Illuminate\Validation\Rule;
use Illuminate\Foundation\Http\FormRequest;

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

    public function rules()
    {
        return [
            'name' => 'required|max:255',
            'email' => [
                'required',
                'email',
                'max:255',
                Rule::unique('users', 'email')->where('company_id', auth()->user()->company_id)->ignore($this->user),
            ],
            'phone' => [
                'required',
                'max:255',
                Rule::unique('users', 'phone')->where('company_id', auth()->user()->company_id)->ignore($this->user),
            ],
            'address' => 'nullable|max:255',
            'birth_date' => 'nullable|date',
            'speak_language' => 'nullable|max:255',
            'employee_id' => [
                'nullable',
                Rule::unique('users', 'employee_id')->where('company_id', auth()->user()->company_id)->ignore($this->user),
            ],
            'joining_date' => 'required|date',
            'department_id' => 'required|exists:departments,id',
            'designation_id' => 'required|exists:designations,id',
            'duty_schedule_id' => 'required|exists:duty_schedules,id',
            'manager_id' => 'nullable|exists:users,id',
            'role_id' => 'required|exists:roles,id',
            'attendance_method' => 'required|array|min:1',
            'weekends' => 'required|array|min:1',
            'weekends.*' => 'in:saturday,sunday,monday,tuesday,wednesday,thursday,friday',
            'avatar' => 'nullable|image|mimes:jpg,jpeg,png,webp|max:2048',
        ];
    }

    public function messages()
    {
        return [
            'name.required' => _trans('validation.Name is required'),
            'email.required' => _trans('validation.Email is required'),
            'email.email' => _trans('validation.Email must be a valid email address'),
            'email.unique' => _trans('validation.Email must be unique'),
            'phone.required' => _trans('validation.Phone is required'),
            'phone.unique' => _trans('validation.Phone must be unique'),
            'gender.required' => _trans('validation.Gender is required'),
            'address.max' => _trans('validation.Address must not exceed 255 characters'),
            'birth_date.date' => _trans('validation.Birth date must be a valid date'),
            'joining_date.required' => _trans('validation.Joining date is required'),
            'department_id.required' => _trans('validation.Department is required'),
            'department_id.exists' => _trans('validation.Department must exist'),
            'designation_id.required' => _trans('validation.Designation is required'),
            'designation_id.exists' => _trans('validation.Designation must exist'),
            'duty_schedule_id.required' => _trans('validation.Duty schedule is required'),
            'duty_schedule_id.exists' => _trans('validation.Duty schedule must exist'),
            'role_id.required' => _trans('validation.Role is required'),
            'role_id.exists' => _trans('validation.Role must exist'),
            'attendance_method.required' => _trans('validation.Attendance method is required'),
            'attendance_method.*.in' => _trans('validation.Invalid attendance method selected'),
            'password_type.required' => _trans('validation.Password type is required'),
            'password_type.in' => _trans('validation.Invalid password type selected'),
            'password.min' => _trans('validation.Password must be at least 8 characters'),
            'weekends.required' => _trans('validation.Weekends are required'),
            'weekends.*.in' => _trans('validation.Invalid weekend selected'),
            'holidays.required' => _trans('validation.Holidays are required'),
            'holidays.*.exists' => _trans('validation.Invalid holiday selected'),
            'avatar.image' => _trans('validation.Avatar must be an image'),
            'avatar.mimes' => _trans('validation.Avatar must be of type jpg, jpeg, png, webp'),
            'avatar.max' => _trans('validation.Avatar size must not exceed 2048 KB'),
        ];
    }
}