<?php

namespace KupShop\GraphQLBundle\ApiAdmin\Util;

use KupShop\GraphQLBundle\ApiAdmin\Types\AdminUser;
use KupShop\GraphQLBundle\ApiAdmin\Types\Collection\AdminUserCollection;
use KupShop\GraphQLBundle\ApiShared\ApiUtil;

class AdminUserUtil
{
    public function getAdminUser(): ?AdminUser
    {
        $adminUser = getAdminUser();

        return ($adminUser) ? new AdminUser($adminUser) : null;
    }

    public function getAdminUsers(int $offset, int $limit): AdminUserCollection
    {
        $admins = sqlQueryBuilder()
            ->select('id, name, login, email, privilege, data')
            ->andWhere(ApiUtil::getLimitSpec($offset, $limit))
            ->addCalcRows()
            ->from('admins')
            ->execute();

        $totalCount = (int) sqlFetchAssoc(sqlQuery('SELECT FOUND_ROWS() as total_count'))['total_count'];

        return (new AdminUserCollection(ApiUtil::wrapItems($admins, AdminUser::class)))
            ->setItemsTotalCount($totalCount)
            ->setLimit($limit)
            ->setOffset($offset);
    }
}
