<?php

declare(strict_types=1);

namespace App\Administration\Application\Query\GetUsers;

use App\Administration\Domain\Model\User\Role;
use App\Administration\Domain\Model\User\User;
use App\Shared\Domain\Clock;
use DateTimeImmutable;

final readonly class UserDto
{
    /**
     * @param string[] $roles
     */
    public function __construct(
        public string $id,
        public string $email,
        public string $role,
        public string $roleLabel,
        /** @var string[] */
        public array $roles,
        public string $firstName,
        public string $lastName,
        public string $statut,
        public DateTimeImmutable $createdAt,
        public ?DateTimeImmutable $invitedAt,
        public ?DateTimeImmutable $activatedAt,
        public ?DateTimeImmutable $blockedAt,
        public ?string $blockedReason,
        public bool $invitationExpiree,
    ) {
    }

    public static function fromDomain(User $user, Clock $clock): self
    {
        return new self(
            id: (string) $user->id,
            email: (string) $user->email,
            role: $user->role->value,
            roleLabel: $user->role->label(),
            roles: array_map(static fn (Role $r) => $r->value, $user->roles),
            firstName: $user->firstName,
            lastName: $user->lastName,
            statut: $user->statut->value,
            createdAt: $user->createdAt,
            invitedAt: $user->invitedAt,
            activatedAt: $user->activatedAt,
            blockedAt: $user->blockedAt,
            blockedReason: $user->blockedReason,
            invitationExpiree: $user->estInvitationExpiree($clock->now()),
        );
    }
}
