<?php

declare(strict_types=1);

namespace Builder\Core\Http\Requests\Account;

use Builder\Core\Extensions\Account\AccountProfile;
use Builder\Core\Foundation\Account\AccountDefinition;
use Builder\Core\Foundation\Middleware\BuilderAuth;
use Builder\Core\Foundation\Registry;
use Illuminate\Foundation\Http\FormRequest;

final class UpdateProfileRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user(BuilderAuth::guard()) !== null;
    }

    /** @return array<string, mixed> */
    public function rules(): array
    {
        return $this->makeDefinition()->getRules();
    }

    /** @return array<string, string> */
    public function messages(): array
    {
        return [
            'name.required' => (string) __('builder::builder.validation.name_required'),
            'email.required' => (string) __('builder::builder.validation.email_required'),
            'email.email' => (string) __('builder::builder.validation.email_invalid'),
            'password.min' => (string) __('builder::builder.validation.password_min'),
            'password.confirmed' => (string) __('builder::builder.validation.password_confirmed'),
        ];
    }

    private function makeDefinition(): AccountDefinition
    {
        return AccountDefinition::make(new AccountProfile, app(Registry::class)->accountExtensions());
    }
}
