prepare($countSql); $countStmt->execute($params); $total = (int) $countStmt->fetchColumn(); // Fetch $sql = "SELECT a.id, a.action_type, a.model_name, a.prompt_tokens, a.completion_tokens, a.total_tokens, a.estimated_cost, a.created_at FROM ai_usage_log a $tenantCondition ORDER BY a.created_at DESC LIMIT $perPage OFFSET $offset"; $stmt = $db->prepare($sql); $stmt->execute($params); $logs = $stmt->fetchAll(\PDO::FETCH_ASSOC); // Translate action types $actionLabels = [ 'invoice_extraction' => 'استخراج فاتورة', 'voice_transcribe' => 'تحويل صوت لنص', 'voice_intent' => 'تحليل أمر صوتي', 'report_generation' => 'توليد تقرير', 'chatbot' => 'محادثة ذكية', ]; foreach ($logs as &$log) { $log['action_label'] = $actionLabels[$log['action_type']] ?? $log['action_type']; $log['estimated_cost'] = round((float) $log['estimated_cost'], 6); } json_success([ 'logs' => $logs, 'pagination' => [ 'page' => $page, 'per_page' => $perPage, 'total' => $total, 'pages' => ceil($total / $perPage), ], ]);