<?php

declare(strict_types=1);

namespace ApiModel\Action;

use ApiModel\Service\OrganizationService;
use ApiModel\Shared\Http\HttpUtil;
use ApiModel\Shared\Support\AuthContext;
use ApiModel\Shared\Exceptions\BadRequestException;
use Psr\Http\Message\ServerRequestInterface;

final class OrganizationAction
{
    public function __construct(private readonly OrganizationService $organization) {}

    /** @return array<string, mixed> */
    public function getMyContext(ServerRequestInterface $request): array
    {
        $u = AuthContext::jwtUser($request);

        return $this->organization->getMyContext($u['userId']);
    }

    /** @return list<array<string, mixed>> */
    public function listCompanies(ServerRequestInterface $request): array
    {
        $u = AuthContext::jwtUser($request);
        AuthContext::requireRoles($u, ['ADMIN', 'ADMINISTRADOR', 'ADMINISTRATOR']);

        return $this->organization->listCompanies($u['userId']);
    }

    /** @return array<string, mixed> */
    public function createCompany(ServerRequestInterface $request): array
    {
        $u = AuthContext::jwtUser($request);
        AuthContext::requireRoles($u, ['ADMIN', 'ADMINISTRADOR', 'ADMINISTRATOR']);

        return $this->organization->createCompany(HttpUtil::parseJson($request));
    }

    /** @return array<string, mixed> */
    public function updateCompany(ServerRequestInterface $request, string $id): array
    {
        $u = AuthContext::jwtUser($request);
        AuthContext::requireRoles($u, ['ADMIN', 'ADMINISTRADOR', 'ADMINISTRATOR']);

        return $this->organization->updateCompany($id, HttpUtil::parseJson($request));
    }

    /** @return list<array<string, mixed>> */
    public function listBranches(ServerRequestInterface $request, string $companyId): array
    {
        $u = AuthContext::jwtUser($request);
        AuthContext::requireRoles($u, ['ADMIN', 'ADMINISTRADOR', 'ADMINISTRATOR']);

        return $this->organization->listBranches($companyId);
    }

    /** @return array<string, mixed> */
    public function createBranch(ServerRequestInterface $request, string $companyId): array
    {
        $u = AuthContext::jwtUser($request);
        AuthContext::requireRoles($u, ['ADMIN', 'ADMINISTRADOR', 'ADMINISTRATOR']);

        return $this->organization->createBranch($companyId, HttpUtil::parseJson($request));
    }

    /** @return array<string, mixed> */
    public function updateBranch(ServerRequestInterface $request, string $id): array
    {
        $u = AuthContext::jwtUser($request);
        AuthContext::requireRoles($u, ['ADMIN', 'ADMINISTRADOR', 'ADMINISTRATOR']);

        return $this->organization->updateBranch($id, HttpUtil::parseJson($request));
    }

    /** @return array{ok: true} */
    public function deleteBranch(ServerRequestInterface $request, string $id): array
    {
        $u = AuthContext::jwtUser($request);
        AuthContext::requireRoles($u, ['ADMIN', 'ADMINISTRADOR', 'ADMINISTRATOR']);

        $this->organization->deleteBranch($id);

        return ['ok' => true];
    }


    /** @return list<array<string, mixed>> */
    public function getUserAccess(ServerRequestInterface $request, string $userId): array
    {
        $u = AuthContext::jwtUser($request);
        AuthContext::requireRoles($u, ['ADMIN', 'ADMINISTRADOR', 'ADMINISTRATOR']);

        return $this->organization->getUserAccessList($userId);
    }

    /** @return array{ok: true} */
    public function setUserAccess(ServerRequestInterface $request, string $userId): array
    {
        $u = AuthContext::jwtUser($request);
        AuthContext::requireRoles($u, ['ADMIN', 'ADMINISTRADOR', 'ADMINISTRATOR']);

        $body = HttpUtil::parseJson($request);
        $access = $body['access'] ?? null;
        if (! is_array($access)) {
            throw new BadRequestException('Se requiere el arreglo access');
        }

        $this->organization->setUserAccess($userId, $access);

        return ['ok' => true];
    }
}
