<?php

namespace App\Http\Requests;

use App\Traits\ConvertsBase64ToFiles;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ProfileRequest extends FormRequest
{
    use ConvertsBase64ToFiles;

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    protected function base64FileKeys(): array
    {
        return [
            'imagen',
        ];
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        return [
            'nombre' => 'required|string|min:3|max:255',
            'apellido' => 'required|string|min:3|max:255',
            'username' => [
                'required',
                'string',
                'min:3',
                'max:255',
                Rule::unique('admins', 'username')->ignore($this->user()?->id),
            ],
            'cedula' => 'nullable|integer',
            'imagen' => [
                'nullable',
                'image',
                'mimes:webp,jpg,png',
                'dimensions:min_width=960,min_height=960',
            ],
            'password' => 'nullable|confirmed|string|min:8|max:25',
        ];
    }

    protected function prepareForValidation()
    {
        $input = $this->input('data.attributes');
        $this->replace($input ?? []);
        $this->convertBase64ToFiles();
    }
}
