{
"tool_response": {
"filePath": "\/var\/www\/dev.campus.systemische-tools.de\/src\/Infrastructure\/Persistence\/CollectionRepository.php",
"oldString": "namespace Infrastructure\\Persistence;\n\nuse Infrastructure\\Config\\DatabaseFactory;\nuse PDO;\n\n\/**\n * Repository for RAG collections stored in ki_dev.rag_collections.\n *\n * Provides access to collection metadata synced from Qdrant.\n * Used for dropdown population and dimension validation.\n *\n * @package Infrastructure\\Persistence\n *\/\nfinal class CollectionRepository\n{",
"newString": "namespace Infrastructure\\Persistence;\n\nuse Domain\\Repository\\CollectionRepositoryInterface;\nuse Infrastructure\\Config\\DatabaseFactory;\nuse PDO;\n\n\/**\n * Repository for RAG collections stored in ki_dev.rag_collections.\n *\n * Provides access to collection metadata synced from Qdrant.\n * Used for dropdown population and dimension validation.\n *\n * @package Infrastructure\\Persistence\n *\/\nfinal class CollectionRepository implements CollectionRepositoryInterface\n{",
"originalFile": "<?php\n\ndeclare(strict_types=1);\n\nnamespace Infrastructure\\Persistence;\n\nuse Infrastructure\\Config\\DatabaseFactory;\nuse PDO;\n\n\/**\n * Repository for RAG collections stored in ki_dev.rag_collections.\n *\n * Provides access to collection metadata synced from Qdrant.\n * Used for dropdown population and dimension validation.\n *\n * @package Infrastructure\\Persistence\n *\/\nfinal class CollectionRepository\n{\n private PDO $pdo;\n\n public function __construct(?PDO $pdo = null)\n {\n $this->pdo = $pdo ?? DatabaseFactory::dev();\n }\n\n \/**\n * Find a collection by its Qdrant collection_id.\n *\n * @return array<string, mixed>|null\n *\/\n public function find(string $collectionId): ?array\n {\n $stmt = $this->pdo->prepare('\n SELECT * FROM rag_collections\n WHERE collection_id = ?\n ');\n $stmt->execute([$collectionId]);\n $result = $stmt->fetch(PDO::FETCH_ASSOC);\n\n return $result ?: null;\n }\n\n \/**\n * Find multiple collections by their IDs.\n *\n * @param array<string> $collectionIds\n * @return array<int, array<string, mixed>>\n *\/\n public function findByIds(array $collectionIds): array\n {\n if (empty($collectionIds)) {\n return [];\n }\n\n $placeholders = implode(',', array_fill(0, count($collectionIds), '?'));\n $stmt = $this->pdo->prepare(\"\n SELECT * FROM rag_collections\n WHERE collection_id IN ($placeholders)\n \");\n $stmt->execute($collectionIds);\n\n return $stmt->fetchAll(PDO::FETCH_ASSOC);\n }\n\n \/**\n * Get all active and searchable collections for dropdowns.\n *\n * @return array<int, array<string, mixed>>\n *\/\n public function getSearchable(): array\n {\n $stmt = $this->pdo->query('\n SELECT collection_id, display_name, points_count, vector_size\n FROM rag_collections\n WHERE is_active = 1 AND is_searchable = 1\n ORDER BY sort_order ASC, display_name ASC\n ');\n\n return $stmt->fetchAll(PDO::FETCH_ASSOC);\n }\n\n \/**\n * Get all active collections (including non-searchable).\n *\n * @return array<int, array<string, mixed>>\n *\/\n public function getActive(): array\n {\n $stmt = $this->pdo->query('\n SELECT collection_id, display_name, points_count, vector_size, is_searchable\n FROM rag_collections\n WHERE is_active = 1\n ORDER BY sort_order ASC, display_name ASC\n ');\n\n return $stmt->fetchAll(PDO::FETCH_ASSOC);\n }\n\n \/**\n * Get all collections (for admin).\n *\n * @return array<int, array<string, mixed>>\n *\/\n public function getAll(): array\n {\n $stmt = $this->pdo->query('\n SELECT * FROM rag_collections\n ORDER BY sort_order ASC, display_name ASC\n ');\n\n return $stmt->fetchAll(PDO::FETCH_ASSOC);\n }\n\n \/**\n * Check if a collection exists.\n *\/\n public function exists(string $collectionId): bool\n {\n $stmt = $this->pdo->prepare('\n SELECT 1 FROM rag_collections WHERE collection_id = ?\n ');\n $stmt->execute([$collectionId]);\n\n return $stmt->fetchColumn() !== false;\n }\n\n \/**\n * Update collection metadata (from Qdrant sync).\n *\n * @param array<string, mixed> $data\n *\/\n public function updateMetadata(string $collectionId, array $data): void\n {\n $allowed = ['vector_size', 'distance_metric', 'points_count', 'last_synced_at'];\n $updates = [];\n $params = [];\n\n foreach ($allowed as $field) {\n if (array_key_exists($field, $data)) {\n $updates[] = \"$field = ?\";\n $params[] = $data[$field];\n }\n }\n\n if (empty($updates)) {\n return;\n }\n\n $params[] = $collectionId;\n $sql = 'UPDATE rag_collections SET ' . implode(', ', $updates) . ' WHERE collection_id = ?';\n $stmt = $this->pdo->prepare($sql);\n $stmt->execute($params);\n }\n\n \/**\n * Create a new collection entry.\n *\n * @param array<string, mixed> $data\n *\/\n public function create(array $data): int\n {\n $stmt = $this->pdo->prepare('\n INSERT INTO rag_collections\n (collection_id, display_name, description, vector_size, distance_metric,\n points_count, embedding_model, source_type, is_active, is_searchable, sort_order)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\n ');\n\n $stmt->execute([\n $data['collection_id'],\n $data['display_name'] ?? $data['collection_id'],\n $data['description'] ?? null,\n $data['vector_size'] ?? null,\n $data['distance_metric'] ?? 'Cosine',\n $data['points_count'] ?? 0,\n $data['embedding_model'] ?? null,\n $data['source_type'] ?? 'manual',\n $data['is_active'] ?? true,\n $data['is_searchable'] ?? true,\n $data['sort_order'] ?? 0,\n ]);\n\n return (int) $this->pdo->lastInsertId();\n }\n\n \/**\n * Deactivate a collection (soft delete).\n *\/\n public function deactivate(string $collectionId): void\n {\n $stmt = $this->pdo->prepare('\n UPDATE rag_collections\n SET is_active = 0, is_searchable = 0\n WHERE collection_id = ?\n ');\n $stmt->execute([$collectionId]);\n }\n\n \/**\n * Check if collection is referenced by sessions, orders, or preferences.\n *\n * @return array{has_references: bool, references: array<int, array{type: string, count: int}>}\n *\/\n public function checkReferences(string $collectionId): array\n {\n $references = [];\n $jsonValue = json_encode($collectionId);\n\n \/\/ Check chat_sessions (ki_content)\n try {\n $pdo = DatabaseFactory::content();\n $stmt = $pdo->prepare(\"\n SELECT COUNT(*) FROM chat_sessions\n WHERE JSON_CONTAINS(collections, ?, '$')\n \");\n $stmt->execute([$jsonValue]);\n $count = (int) $stmt->fetchColumn();\n if ($count > 0) {\n $references[] = ['type' => 'chat_sessions', 'count' => $count];\n }\n\n \/\/ Check content_orders (ki_content)\n $stmt = $pdo->prepare(\"\n SELECT COUNT(*) FROM content_orders\n WHERE JSON_CONTAINS(collections, ?, '$')\n \");\n $stmt->execute([$jsonValue]);\n $count = (int) $stmt->fetchColumn();\n if ($count > 0) {\n $references[] = ['type' => 'content_orders', 'count' => $count];\n }\n } catch (\\PDOException $e) {\n \/\/ Tables might not exist yet\n }\n\n return [\n 'has_references' => count($references) > 0,\n 'references' => $references,\n ];\n }\n}\n",
"structuredPatch": [
{
"oldStart": 4,
"oldLines": 6,
"newStart": 4,
"newLines": 7,
"lines": [
" ",
" namespace Infrastructure\\Persistence;",
" ",
"+use Domain\\Repository\\CollectionRepositoryInterface;",
" use Infrastructure\\Config\\DatabaseFactory;",
" use PDO;",
" "
]
},
{
"oldStart": 15,
"oldLines": 7,
"newStart": 16,
"newLines": 7,
"lines": [
" *",
" * @package Infrastructure\\Persistence",
" *\/",
"-final class CollectionRepository",
"+final class CollectionRepository implements CollectionRepositoryInterface",
" {",
" private PDO $pdo;",
" "
]
}
],
"userModified": false,
"replaceAll": false
}
}