<?php

namespace App\Domains\Auth\Services;

use App\Domains\Auth\Models\User;
use App\Domains\Notification\Events\UserRegistered;

class RegisterUserService
{
    /**
     * @param  array{tenant_codigo?: string|null, nombre_apellido: string, email: string, password: string, dni?: string|null, telefono?: string|null}  $data
     */
    public function register(array $data): User
    {
        $user = User::query()->create([
            'nombre_apellido' => $data['nombre_apellido'],
            'email' => $data['email'],
            'password' => $data['password'],
            'dni' => $data['dni'] ?? null,
            'telefono' => $data['telefono'] ?? null,
        ]);

        if (! empty($data['tenant_codigo'])) {
            UserRegistered::dispatch($user, $data['tenant_codigo']);
        }

        return $user;
    }
}
