userModel = new UserModel(); $this->agentsModel = new AgentsModel(); } public function __invoke(ServerRequestInterface $request) { $userId = (int) ($request->getAttribute('user_id') ?? 0); $body = json_decode((string) $request->getBody(), true) ?: []; $agentId = (int) ($body['id'] ?? 0); if ($userId <= 0) { return ResponseLib::sendFail('Unauthorized: Missing authenticated user', [], 'E_VALIDATE')->withStatus(401); } if ($agentId <= 0) { return ResponseLib::sendFail('Missing or invalid id', [], 'E_VALIDATE')->withStatus(400); } try { $companyId = $this->userModel->getCompanyIdByUserId($userId); if ($companyId === null) { return ResponseLib::sendFail('User not found', [], 'E_NOT_FOUND')->withStatus(404); } $agent = $this->agentsModel->toggleAgentStatus($companyId, $agentId); if ($agent === null) { return ResponseLib::sendFail('Agent not found', [], 'E_NOT_FOUND')->withStatus(404); } return ResponseLib::sendOk($agent); } catch (\Throwable $e) { Logger::error('Failed to update agent status', ['error' => $e->getMessage()]); return ResponseLib::sendFail('Failed to update agent status', [], 'E_GENERIC')->withStatus(500); } } }