<?php
namespace Incoviba\Controller\API\Admin;

use Exception;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Controller\API\withJson;
use Incoviba\Service;

class Users
{
    use withJson;

    public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Login $loginService): ResponseInterface
    {
        $body = $request->getParsedBody();
        $output = [
            'input' => array_filter($body, fn($key) => $key !== 'password', ARRAY_FILTER_USE_KEY),
            'success' => false,
            'user' => null
        ];
        try {
            $user = $loginService->addUser($body);
            $output['success'] = true;
            $output['user'] = $user;
        } catch (EmptyResult) {}
        return $this->withJson($response, $output);
    }
}
