<?php

namespace KupShop\GraphQLBundle\ApiShared\Types\User;

use KupShop\KupShopBundle\Context\ContextManager;
use KupShop\KupShopBundle\Context\UserContext;
use KupShop\KupShopBundle\Util\Contexts;
use TheCodingMachine\GraphQLite\Annotations\Autowire;
use TheCodingMachine\GraphQLite\Annotations\Field;

class User
{
    public $user;

    public function __construct(\User $user)
    {
        $this->user = $user;
    }

    /**
     * ID uživatele.
     *
     * @Field()
     */
    public function getId(): int
    {
        return (int) $this->user->id;
    }

    /**
     * E-mail.
     *
     * @Field()
     */
    public function getEmail(): string
    {
        return $this->user->email;
    }

    /**
     * Jméno.
     *
     * @Field()
     */
    public function getName(): string
    {
        return $this->user->name ?? '';
    }

    /**
     * Příjmení.
     *
     * @Field()
     */
    public function getSurname(): string
    {
        return $this->user->surname ?? '';
    }

    /**
     * Celé jméno.
     *
     * @Field()
     */
    public function getUserName(): string
    {
        return implode(' ', [$this->getName(), $this->getSurname()]);
    }

    /**
     * Vrací, zda je uživatel B2B.
     *
     * @Field()
     *
     * @Autowire(for="$contextManager")
     */
    public function getIsB2B(ContextManager $contextManager): bool
    {
        return $contextManager->activateContexts([UserContext::class => $this->user], function () {
            /** @var UserContext $userContext */
            $userContext = Contexts::get(UserContext::class);

            return $userContext->isDealer();
        });
    }
}
