<?php

namespace KupShop\GraphQLBundle\ApiAdmin\Controller;

use KupShop\GraphQLBundle\ApiAdmin\Annotation\Module;
use KupShop\GraphQLBundle\ApiAdmin\Types\Collection\UserBonusPointCollection;
use KupShop\GraphQLBundle\ApiAdmin\Types\Collection\UserCollection;
use KupShop\GraphQLBundle\ApiAdmin\Types\Parameters;
use KupShop\GraphQLBundle\ApiAdmin\Types\User\Input\UserAddressInput;
use KupShop\GraphQLBundle\ApiAdmin\Types\User\Input\UserBonusPointFilterInput;
use KupShop\GraphQLBundle\ApiAdmin\Types\User\Input\UserGroupsRelationInput;
use KupShop\GraphQLBundle\ApiAdmin\Types\User\Input\UserInput;
use KupShop\GraphQLBundle\ApiAdmin\Types\User\Response\UserAddressMutateResponse;
use KupShop\GraphQLBundle\ApiAdmin\Types\User\Response\UserMutateResponse;
use KupShop\GraphQLBundle\ApiAdmin\Types\User\User;
use KupShop\GraphQLBundle\ApiAdmin\Util\UserUtil;
use Symfony\Contracts\Service\Attribute\Required;
use TheCodingMachine\GraphQLite\Annotations\Mutation;
use TheCodingMachine\GraphQLite\Annotations\Query;
use TheCodingMachine\GraphQLite\Annotations\UseInputType;

class UserController
{
    protected UserUtil $userUtil;

    #[Required]
    public function setUserUtil(UserUtil $userUtil): void
    {
        $this->userUtil = $userUtil;
    }

    /**
     * Načte uživatele buď podle ID, e-mailu nebo ID košíku uživatele.
     */
    #[Query]
    public function user(?int $id, ?string $email, ?string $cartId): ?User
    {
        return $this->userUtil->getUser($id, $email, $cartId);
    }

    /**
     * Seznam uživatelů.
     */
    #[Query]
    public function users(
        int $offset = 0,
        int $limit = 100,
        #[UseInputType('UserSortInput')] ?Parameters $userSort = null,
        #[UseInputType('UserFilterInput')] ?Parameters $userFilter = null,
    ): UserCollection {
        return $this->userUtil->getUserCollection($offset, $limit, $userSort, $userFilter);
    }

    /**
     * Historie bodů uživatelů.
     */
    #[Query]
    #[Module(\Modules::BONUS_PROGRAM)]
    public function usersBonusPoints(
        int $offset = 0,
        int $limit = 100,
        #[UseInputType('UserBonusPointSortInput')] ?Parameters $sort = null,
        ?UserBonusPointFilterInput $filter = null,
    ): UserBonusPointCollection {
        return $this->userUtil->getUsersBonusPoints(
            offset: $offset,
            limit: $limit,
            sort: $sort,
            filter: $filter,
        );
    }

    /**
     * Vytvoří uživatele.
     *
     * @deprecated Use `userCreateOrUpdate`
     */
    #[Mutation]
    public function userCreate(UserInput $input): UserMutateResponse
    {
        return $this->userUtil->createOrUpdateUser($input);
    }

    /**
     * Aktualizuje uživatele.
     *
     * @deprecated Use `userCreateOrUpdate`
     */
    #[Mutation]
    public function userUpdate(UserInput $input): UserMutateResponse
    {
        return $this->userUtil->createOrUpdateUser($input);
    }

    /**
     * Vytvoří / aktualizuje uživatele.
     */
    #[Mutation]
    public function userCreateOrUpdate(UserInput $input): UserMutateResponse
    {
        return $this->userUtil->createOrUpdateUser($input);
    }

    /**
     * Vytvoří / aktualizuje doručovací adresu uživatele.
     */
    #[Mutation]
    #[Module(\Modules::USER_ADDRESSES)]
    public function userAddressCreateOrUpdate(UserAddressInput $input): UserAddressMutateResponse
    {
        return $this->userUtil->createOrUpdateUserAddress($input);
    }

    /**
     * Přidá / odebere uživatelské skupiny uživateli.
     */
    #[Mutation]
    public function userGroupsRelationUpdate(UserGroupsRelationInput $input): UserMutateResponse
    {
        return $this->userUtil->addOrRemoveUserGroupsRelations($input);
    }
}
