<?php

namespace App\Http\Requests;


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

#[OA\Schema(
    schema: 'ChangePasswordRequest',
    required: ['password', 'new_password', 'status', 'message', 'errors'],
    properties: [
        new OA\Property(property: 'password', type: 'string', format: 'password'),
        new OA\Property(property: 'new_password', type: 'string', format: 'password'),
        new OA\Property(property: 'status', type: 'string'),
        new OA\Property(property: 'message', type: 'string'),
        new OA\Property(property: 'errors', type: 'string')
    ]
)]
class ChangePasswordRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'password' => ['required', 'string', 'min:6'], // current password
            'new_password' => ['required', 'string',  Password::default(), 'confirmed'], // new password with confirmation
        ];
    }

    public function messages(): array
    {
        return [
            // Current Password
            'password.required' => __('validation.change_password.password_required'),
            'password.string' => __('validation.change_password.password_string'),
            'password.min' => __('validation.change_password.password_min'),

            // New Password
            'new_password.required' => __('validation.change_password.new_password_required'),
            'new_password.string' => __('validation.change_password.new_password_string'),
            'new_password.min' => __('validation.change_password.new_password_min'),
            'new_password.confirmed' => __('validation.change_password.new_password_confirmed'),
        ];
    }

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