<?php

namespace App\Http\Requests\Api\V1\Member;

use App\Http\Requests\Api\ApiFormRequest;
use App\Models\Member;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Validation\Rule;

class UpdateMemberProfileRequest extends ApiFormRequest
{
    public function rules(): array
    {
        $member = $this->user();
        $memberId = $member instanceof Member ? $member->id : 0;

        return [
            'name'  => ['sometimes', 'string', 'max:255'],
            'email' => [
                'sometimes',
                'nullable',
                'email',
                'max:255',
                Rule::unique('members', 'email')->ignore($memberId),
            ],
            'address' => ['sometimes', 'nullable', 'string', 'max:500'],
        ];
    }

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