<?php

declare(strict_types=1);

namespace App\Administration\Infrastructure\Security;

use App\Administration\Domain\Model\User\Role;
use App\Administration\Domain\Model\User\User as DomainUser;
use LogicException;

/**
 * Factory pour créer des SecurityUser depuis des Domain Users.
 *
 * Respecte le principe "No Static" d'Elegant Objects.
 *
 * @see Story 1.4 - Connexion utilisateur
 */
final readonly class SecurityUserFactory
{
    public function fromDomainUser(DomainUser $domainUser): SecurityUser
    {
        if ($domainUser->email === null) {
            throw new LogicException('Cannot create SecurityUser from a domain user without email.');
        }

        /** @var non-empty-string $email */
        $email = (string) $domainUser->email;

        return new SecurityUser(
            userId: $domainUser->id,
            email: $email,
            hashedPassword: $domainUser->hashedPassword ?? '',
            tenantId: $domainUser->tenantId,
            roles: array_values(array_map(
                static fn (Role $role) => $role->value,
                $domainUser->roles,
            )),
        );
    }
}
