false, 'error' => 'document_id is required']); exit; } $ragDb = dbnToolsRagDb(); $docStmt = $ragDb->prepare(" SELECT d.id, d.title, ds.summary AS ai_summary, ds.summary_model FROM documents d LEFT JOIN doc_summaries ds ON ds.document_id = d.id WHERE d.id = ? LIMIT 1 "); $docStmt->execute([$documentId]); $doc = $docStmt->fetch(PDO::FETCH_ASSOC); if (!$doc) { echo json_encode(['ok' => false, 'error' => 'Document not found']); exit; } $chunkStmt = $ragDb->prepare(" SELECT chunk_index, section_title, content FROM chunks WHERE document_id = ? ORDER BY chunk_index ASC "); $chunkStmt->execute([$documentId]); $chunks = []; foreach ($chunkStmt as $row) { $chunks[] = [ 'chunk_index' => (int)$row['chunk_index'], 'section_title' => $row['section_title'] ?? null, 'content' => (string)$row['content'], ]; } echo json_encode([ 'ok' => true, 'document' => [ 'id' => (int)$doc['id'], 'title' => (string)$doc['title'], 'ai_summary' => $doc['ai_summary'] ?? null, 'summary_model' => $doc['summary_model'] ?? null, ], 'chunks' => $chunks, ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); } catch (Throwable $e) { error_log('DBN document-chunks error: ' . $e->getMessage()); echo json_encode(['ok' => false, 'error' => 'Internal error']); }