<?php

namespace App\Dto\Output;

use ApiPlatform\Metadata\Get;
use App\Entity\OrganizationalUnit;
use App\Entity\User;
use App\Entity\UserGroup;
use Symfony\Component\Serializer\Annotation\Groups;

#[Get(shortName: 'User')]
final class UserOutput extends AbstractOutput
{
    #[Groups(['user:read'])]
    public string $username;

    #[Groups(['user:read'])]
    public array $roles;

    #[Groups(['user:read'])]
    public bool $enabled;
    #[Groups(['user:read'])]
    public array $allowedOrganizationalUnits;
    #[Groups(['user:read'])]
    public array $userGroups;
    #[Groups(['user:read'])]
    public ?string $groupsView = 'card';

    #[Groups(['user:read'])]
    public \DateTime $createdAt;

    #[Groups(['user:read'])]
    public ?string $createdBy = null;

    public function __construct(User $user)
    {
        parent::__construct($user);

        $this->username = $user->getUsername();
        $this->roles = $user->getRoles();
        $this->enabled = $user->isEnabled();
        $this->groupsView = $user->getGroupsView();

        $this->userGroups = $user->getUserGroups()->map(
            fn(UserGroup $userGroup) => new UserGroupOutput($userGroup)
        )->toArray();

        $this->allowedOrganizationalUnits = $user->getAllowedOrganizationalUnits()->map(
            fn(OrganizationalUnit $organizationalUnit) => new OrganizationalUnitOutput($organizationalUnit)
        )->toArray();

        $this->createdAt = $user->getCreatedAt();
        $this->createdBy = $user->getCreatedBy();
    }
}