| | | 1 | | using EHonda.KicktippAi.Core; |
| | | 2 | | using FirebaseAdapter.Models; |
| | | 3 | | using Google.Cloud.Firestore; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | using NodaTime; |
| | | 6 | | |
| | | 7 | | namespace FirebaseAdapter; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Firebase Firestore implementation of the context repository. |
| | | 11 | | /// </summary> |
| | | 12 | | public class FirebaseContextRepository : IContextRepository |
| | | 13 | | { |
| | | 14 | | private readonly FirestoreDb _firestoreDb; |
| | | 15 | | private readonly ILogger<FirebaseContextRepository> _logger; |
| | | 16 | | private readonly string _contextDocumentsCollection; |
| | | 17 | | private readonly string _competition; |
| | | 18 | | |
| | 1 | 19 | | public FirebaseContextRepository(FirestoreDb firestoreDb, ILogger<FirebaseContextRepository> logger) |
| | | 20 | | { |
| | 1 | 21 | | _firestoreDb = firestoreDb ?? throw new ArgumentNullException(nameof(firestoreDb)); |
| | 1 | 22 | | _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| | | 23 | | |
| | 1 | 24 | | _contextDocumentsCollection = "context-documents"; |
| | 1 | 25 | | _competition = "bundesliga-2025-26"; |
| | | 26 | | |
| | 1 | 27 | | _logger.LogInformation("Firebase context repository initialized"); |
| | 1 | 28 | | } |
| | | 29 | | |
| | | 30 | | public async Task<int?> SaveContextDocumentAsync(string documentName, string content, string communityContext, Cance |
| | | 31 | | { |
| | | 32 | | try |
| | | 33 | | { |
| | | 34 | | // Get the latest version to check if content differs |
| | 1 | 35 | | var latestDocument = await GetLatestContextDocumentAsync(documentName, communityContext, cancellationToken); |
| | | 36 | | |
| | | 37 | | // If content is the same, don't save a new version |
| | 1 | 38 | | if (latestDocument != null && latestDocument.Content == content) |
| | | 39 | | { |
| | 1 | 40 | | _logger.LogInformation("Context document {DocumentName} content unchanged, skipping save", documentName) |
| | 1 | 41 | | return null; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | // Determine the next version number |
| | 1 | 45 | | var nextVersion = latestDocument?.Version + 1 ?? 0; |
| | | 46 | | |
| | 1 | 47 | | var now = Timestamp.GetCurrentTimestamp(); |
| | 1 | 48 | | var documentId = $"{documentName}_{communityContext}_{nextVersion}"; |
| | | 49 | | |
| | 1 | 50 | | var firestoreDocument = new FirestoreContextDocument |
| | 1 | 51 | | { |
| | 1 | 52 | | Id = documentId, |
| | 1 | 53 | | DocumentName = documentName, |
| | 1 | 54 | | Content = content, |
| | 1 | 55 | | Version = nextVersion, |
| | 1 | 56 | | CreatedAt = now, |
| | 1 | 57 | | Competition = _competition, |
| | 1 | 58 | | CommunityContext = communityContext |
| | 1 | 59 | | }; |
| | | 60 | | |
| | 1 | 61 | | var docRef = _firestoreDb.Collection(_contextDocumentsCollection).Document(documentId); |
| | 1 | 62 | | await docRef.SetAsync(firestoreDocument, cancellationToken: cancellationToken); |
| | | 63 | | |
| | 1 | 64 | | _logger.LogInformation("Saved context document {DocumentName} version {Version} for community {CommunityCont |
| | 1 | 65 | | documentName, nextVersion, communityContext); |
| | | 66 | | |
| | 1 | 67 | | return nextVersion; |
| | | 68 | | } |
| | 0 | 69 | | catch (Exception ex) |
| | | 70 | | { |
| | 0 | 71 | | _logger.LogError(ex, "Failed to save context document {DocumentName} for community {CommunityContext}", |
| | 0 | 72 | | documentName, communityContext); |
| | 0 | 73 | | throw; |
| | | 74 | | } |
| | 1 | 75 | | } |
| | | 76 | | |
| | | 77 | | public async Task<ContextDocument?> GetLatestContextDocumentAsync(string documentName, string communityContext, Canc |
| | | 78 | | { |
| | | 79 | | try |
| | | 80 | | { |
| | 1 | 81 | | var query = _firestoreDb.Collection(_contextDocumentsCollection) |
| | 1 | 82 | | .WhereEqualTo("documentName", documentName) |
| | 1 | 83 | | .WhereEqualTo("communityContext", communityContext) |
| | 1 | 84 | | .WhereEqualTo("competition", _competition) |
| | 1 | 85 | | .OrderByDescending("version") |
| | 1 | 86 | | .Limit(1); |
| | | 87 | | |
| | 1 | 88 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | | 89 | | |
| | 1 | 90 | | if (!snapshot.Documents.Any()) |
| | | 91 | | { |
| | 1 | 92 | | return null; |
| | | 93 | | } |
| | | 94 | | |
| | 1 | 95 | | var firestoreDoc = snapshot.Documents.First().ConvertTo<FirestoreContextDocument>(); |
| | 1 | 96 | | return ConvertToContextDocument(firestoreDoc); |
| | | 97 | | } |
| | 0 | 98 | | catch (Exception ex) |
| | | 99 | | { |
| | 0 | 100 | | _logger.LogError(ex, "Failed to retrieve latest context document {DocumentName} for community {CommunityCont |
| | 0 | 101 | | documentName, communityContext); |
| | 0 | 102 | | throw; |
| | | 103 | | } |
| | 1 | 104 | | } |
| | | 105 | | |
| | | 106 | | public async Task<ContextDocument?> GetContextDocumentAsync(string documentName, int version, string communityContex |
| | | 107 | | { |
| | | 108 | | try |
| | | 109 | | { |
| | 1 | 110 | | var documentId = $"{documentName}_{communityContext}_{version}"; |
| | 1 | 111 | | var docRef = _firestoreDb.Collection(_contextDocumentsCollection).Document(documentId); |
| | 1 | 112 | | var snapshot = await docRef.GetSnapshotAsync(cancellationToken); |
| | | 113 | | |
| | 1 | 114 | | if (!snapshot.Exists) |
| | | 115 | | { |
| | 1 | 116 | | return null; |
| | | 117 | | } |
| | | 118 | | |
| | 1 | 119 | | var firestoreDoc = snapshot.ConvertTo<FirestoreContextDocument>(); |
| | 1 | 120 | | return ConvertToContextDocument(firestoreDoc); |
| | | 121 | | } |
| | 0 | 122 | | catch (Exception ex) |
| | | 123 | | { |
| | 0 | 124 | | _logger.LogError(ex, "Failed to retrieve context document {DocumentName} version {Version} for community {Co |
| | 0 | 125 | | documentName, version, communityContext); |
| | 0 | 126 | | throw; |
| | | 127 | | } |
| | 1 | 128 | | } |
| | | 129 | | |
| | | 130 | | public async Task<IReadOnlyList<string>> GetContextDocumentNamesAsync(string communityContext, CancellationToken can |
| | | 131 | | { |
| | | 132 | | try |
| | | 133 | | { |
| | 1 | 134 | | var query = _firestoreDb.Collection(_contextDocumentsCollection) |
| | 1 | 135 | | .WhereEqualTo("communityContext", communityContext) |
| | 1 | 136 | | .WhereEqualTo("competition", _competition) |
| | 1 | 137 | | .Select("documentName"); |
| | | 138 | | |
| | 1 | 139 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | | 140 | | |
| | 1 | 141 | | var documentNames = snapshot.Documents |
| | 1 | 142 | | .Select(doc => doc.GetValue<string>("documentName")) |
| | 1 | 143 | | .Distinct() |
| | 1 | 144 | | .ToList() |
| | 1 | 145 | | .AsReadOnly(); |
| | | 146 | | |
| | 1 | 147 | | return documentNames; |
| | | 148 | | } |
| | 0 | 149 | | catch (Exception ex) |
| | | 150 | | { |
| | 0 | 151 | | _logger.LogError(ex, "Failed to retrieve context document names for community {CommunityContext}", community |
| | 0 | 152 | | throw; |
| | | 153 | | } |
| | 1 | 154 | | } |
| | | 155 | | |
| | | 156 | | public async Task<IReadOnlyList<ContextDocument>> GetContextDocumentVersionsAsync(string documentName, string commun |
| | | 157 | | { |
| | | 158 | | try |
| | | 159 | | { |
| | 1 | 160 | | var query = _firestoreDb.Collection(_contextDocumentsCollection) |
| | 1 | 161 | | .WhereEqualTo("documentName", documentName) |
| | 1 | 162 | | .WhereEqualTo("communityContext", communityContext) |
| | 1 | 163 | | .WhereEqualTo("competition", _competition) |
| | 1 | 164 | | .OrderBy("version"); |
| | | 165 | | |
| | 1 | 166 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | | 167 | | |
| | 1 | 168 | | var documents = snapshot.Documents |
| | 1 | 169 | | .Select(doc => doc.ConvertTo<FirestoreContextDocument>()) |
| | 1 | 170 | | .Select(ConvertToContextDocument) |
| | 1 | 171 | | .ToList() |
| | 1 | 172 | | .AsReadOnly(); |
| | | 173 | | |
| | 1 | 174 | | return documents; |
| | | 175 | | } |
| | 0 | 176 | | catch (Exception ex) |
| | | 177 | | { |
| | 0 | 178 | | _logger.LogError(ex, "Failed to retrieve context document versions for {DocumentName} in community {Communit |
| | 0 | 179 | | documentName, communityContext); |
| | 0 | 180 | | throw; |
| | | 181 | | } |
| | 1 | 182 | | } |
| | | 183 | | |
| | | 184 | | public async Task<bool> UpdateContextDocumentVersionAsync(string documentName, int version, string newContent, strin |
| | | 185 | | { |
| | | 186 | | try |
| | | 187 | | { |
| | 1 | 188 | | var documentId = $"{documentName}_{communityContext}_{version}"; |
| | 1 | 189 | | var docRef = _firestoreDb.Collection(_contextDocumentsCollection).Document(documentId); |
| | | 190 | | |
| | | 191 | | // Check if document exists |
| | 1 | 192 | | var snapshot = await docRef.GetSnapshotAsync(cancellationToken); |
| | 1 | 193 | | if (!snapshot.Exists) |
| | | 194 | | { |
| | 1 | 195 | | _logger.LogWarning("Cannot update non-existent document {DocumentId}", documentId); |
| | 1 | 196 | | return false; |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | // Update only the content field |
| | 1 | 200 | | var updates = new Dictionary<string, object> |
| | 1 | 201 | | { |
| | 1 | 202 | | ["content"] = newContent |
| | 1 | 203 | | }; |
| | | 204 | | |
| | 1 | 205 | | await docRef.UpdateAsync(updates, cancellationToken: cancellationToken); |
| | | 206 | | |
| | 1 | 207 | | _logger.LogInformation("Updated content for context document {DocumentName} version {Version} in community { |
| | 1 | 208 | | documentName, version, communityContext); |
| | | 209 | | |
| | 1 | 210 | | return true; |
| | | 211 | | } |
| | 0 | 212 | | catch (Exception ex) |
| | | 213 | | { |
| | 0 | 214 | | _logger.LogError(ex, "Failed to update context document {DocumentName} version {Version} in community {Commu |
| | 0 | 215 | | documentName, version, communityContext); |
| | 0 | 216 | | throw; |
| | | 217 | | } |
| | 1 | 218 | | } |
| | | 219 | | |
| | | 220 | | private static ContextDocument ConvertToContextDocument(FirestoreContextDocument firestoreDoc) |
| | | 221 | | { |
| | 1 | 222 | | return new ContextDocument( |
| | 1 | 223 | | firestoreDoc.DocumentName, |
| | 1 | 224 | | firestoreDoc.Content, |
| | 1 | 225 | | firestoreDoc.Version, |
| | 1 | 226 | | firestoreDoc.CreatedAt.ToDateTimeOffset()); |
| | | 227 | | } |
| | | 228 | | } |