gaService = $gaService; $this->llmService = $llmService; $this->dataProcessor = $dataProcessor; } /** * Get all available reports * GET /api/reports */ public function getReports(IRequest $request): DataResponse { $this->validateAgentAccess(); $reports = $this->gaService->getAllReports(); return new DataResponse([ 'success' => true, 'data' => [ 'reports' => $reports ] ]); } /** * Get specific report by ID * GET /api/report/{id} */ public function getReport(IRequest $request, int $id): DataResponse { $this->validateAgentAccess(); $report = $this->gaService->getReportById($id); if (!$report) { return new DataResponse([ 'success' => false, 'error' => 'Report not found' ], Http::STATUS_NOT_FOUND); } return new DataResponse([ 'success' => true, 'data' => [ 'report' => $report ] ]); } /** * Generate new report * POST /api/generate */ public function generateReport(IRequest $request): JSONResponse { $this->validateAgentAccess(); $params = $request->getParams(); $clientSlug = $params['client_slug'] ?? null; $dateRange = $params['date_range'] ?? '7d'; if (!$clientSlug) { return new JSONResponse([ 'success' => false, 'error' => 'client_slug is required' ], Http::STATUS_BAD_REQUEST); } try { // Get client configuration $client = $this->gaService->getClientBySlug($clientSlug); if (!$client) { return new JSONResponse([ 'success' => false, 'error' => 'Client not found' ], Http::STATUS_NOT_FOUND); } // Fetch GA4 data $rawData = $this->gaService->fetchGA4Data($client, $dateRange); // Process and validate $processed = $this->dataProcessor->process($rawData, $client); // Generate report via LLM $markdown = $this->llmService->generate($processed, $client); // Save report to Nextcloud $report = $this->gaService->saveReport($client, $markdown); return new JSONResponse([ 'success' => true, 'data' => [ 'report_id' => $report->getId(), 'report_date' => date('Y-m-d'), 'file_path' => $report->getFilePath(), 'markdown_preview' => substr($markdown, 0, 500) . '...' ] ]); } catch (\Exception $e) { \OCP\Util::writeLog("Generate report failed: {$e->getMessage()}"); return new JSONResponse([ 'success' => false, 'error' => $e->getMessage() ], Http::STATUS_INTERNAL_SERVER_ERROR); } } /** * Get app status * GET /api/status */ public function getStatus(IRequest $request): DataResponse { $this->validateAgentAccess(); $status = [ 'app_name' => AppInfo::APP_NAME, 'version' => AppInfo::getVersion(), 'status' => 'operational', 'google_analytics' => $this->gaService->isConfigured() ? 'configured' : 'not_configured', 'llm_service' => $this->llmService->isConfigured() ? 'configured' : 'not_configured', 'total_clients' => $this->gaService->getClientCount(), 'last_report_time' => $this->gaService->getLastReportTime() ]; return new DataResponse([ 'success' => true, 'data' => $status ]); } /** * Validate agent access using app password */ private function validateAgentAccess(): void { // Authentication will be handled by Nextcloud middleware // This is a placeholder for future enhancement } }