| | | 1 | | using Chronicis.Client.Models; |
| | | 2 | | using Chronicis.Shared.DTOs; |
| | | 3 | | using Chronicis.Shared.Enums; |
| | | 4 | | |
| | | 5 | | namespace Chronicis.Client.Services.Tree; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Handles CRUD operations for tree nodes: create, move, delete, and update. |
| | | 9 | | /// This component makes API calls and coordinates with TreeUiState for post-mutation state updates. |
| | | 10 | | /// </summary> |
| | | 11 | | internal sealed class TreeMutations |
| | | 12 | | { |
| | | 13 | | private readonly IArticleApiService _articleApi; |
| | | 14 | | private readonly IAppContextService _appContext; |
| | | 15 | | private readonly ILogger _logger; |
| | | 16 | | |
| | | 17 | | // Shared node index (owned by TreeStateService, passed in) |
| | 27 | 18 | | private TreeNodeIndex _nodeIndex = new(); |
| | | 19 | | |
| | | 20 | | // Callback to trigger tree refresh after mutations |
| | | 21 | | private Func<Task>? _refreshCallback; |
| | | 22 | | |
| | 27 | 23 | | public TreeMutations( |
| | 27 | 24 | | IArticleApiService articleApi, |
| | 27 | 25 | | IAppContextService appContext, |
| | 27 | 26 | | ILogger logger) |
| | | 27 | | { |
| | 27 | 28 | | _articleApi = articleApi; |
| | 27 | 29 | | _appContext = appContext; |
| | 27 | 30 | | _logger = logger; |
| | 27 | 31 | | } |
| | | 32 | | |
| | | 33 | | // ============================================ |
| | | 34 | | // Initialization |
| | | 35 | | // ============================================ |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Sets the node index reference. Called after tree is built. |
| | | 39 | | /// </summary> |
| | | 40 | | public void SetNodeIndex(TreeNodeIndex nodeIndex) |
| | | 41 | | { |
| | 27 | 42 | | _nodeIndex = nodeIndex; |
| | 27 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Sets the callback to invoke when tree needs to be refreshed after a mutation. |
| | | 47 | | /// </summary> |
| | | 48 | | public void SetRefreshCallback(Func<Task> refreshCallback) |
| | | 49 | | { |
| | 27 | 50 | | _refreshCallback = refreshCallback; |
| | 27 | 51 | | } |
| | | 52 | | |
| | | 53 | | // ============================================ |
| | | 54 | | // Create Operations |
| | | 55 | | // ============================================ |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Creates a new root-level article in the current world. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <returns>The ID of the created article, or null if creation failed.</returns> |
| | | 61 | | public async Task<Guid?> CreateRootArticleAsync() |
| | | 62 | | { |
| | 2 | 63 | | var worldId = _appContext.CurrentWorldId; |
| | 2 | 64 | | if (!worldId.HasValue) |
| | | 65 | | { |
| | 1 | 66 | | _logger.LogWarning("Cannot create root article: no world selected"); |
| | 1 | 67 | | return null; |
| | | 68 | | } |
| | | 69 | | |
| | 1 | 70 | | var createDto = new ArticleCreateDto |
| | 1 | 71 | | { |
| | 1 | 72 | | Title = string.Empty, |
| | 1 | 73 | | Body = string.Empty, |
| | 1 | 74 | | ParentId = null, |
| | 1 | 75 | | WorldId = worldId, |
| | 1 | 76 | | Type = ArticleType.WikiArticle, |
| | 1 | 77 | | EffectiveDate = DateTime.Now |
| | 1 | 78 | | }; |
| | | 79 | | |
| | 1 | 80 | | var created = await _articleApi.CreateArticleAsync(createDto); |
| | 1 | 81 | | if (created == null) |
| | | 82 | | { |
| | 0 | 83 | | _logger.LogWarning("Failed to create root article"); |
| | 0 | 84 | | return null; |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | // Refresh tree to show new article |
| | 1 | 88 | | if (_refreshCallback != null) |
| | | 89 | | { |
| | 1 | 90 | | await _refreshCallback(); |
| | | 91 | | } |
| | | 92 | | |
| | 1 | 93 | | return created.Id; |
| | 2 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Creates a new child article under the specified parent. |
| | | 98 | | /// </summary> |
| | | 99 | | /// <param name="parentId">The parent node ID (can be article, arc, or virtual group).</param> |
| | | 100 | | /// <returns>The ID of the created article, or null if creation failed.</returns> |
| | | 101 | | public async Task<Guid?> CreateChildArticleAsync(Guid parentId) |
| | | 102 | | { |
| | 2 | 103 | | if (!_nodeIndex.TryGetNode(parentId, out var parentNode) || parentNode == null) |
| | | 104 | | { |
| | 1 | 105 | | _logger.LogWarning("Cannot create child: parent {ParentId} not found", parentId); |
| | 1 | 106 | | return null; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | // Determine article type based on parent |
| | 1 | 110 | | var articleType = DetermineChildArticleType(parentNode); |
| | | 111 | | |
| | | 112 | | // For virtual groups, parent is null (top-level in that category) |
| | 1 | 113 | | Guid? actualParentId = parentNode.NodeType == TreeNodeType.VirtualGroup ? null : parentId; |
| | | 114 | | |
| | 1 | 115 | | var createDto = new ArticleCreateDto |
| | 1 | 116 | | { |
| | 1 | 117 | | Title = string.Empty, |
| | 1 | 118 | | Body = string.Empty, |
| | 1 | 119 | | ParentId = actualParentId, |
| | 1 | 120 | | WorldId = parentNode.WorldId ?? _appContext.CurrentWorldId, |
| | 1 | 121 | | CampaignId = parentNode.CampaignId, |
| | 1 | 122 | | ArcId = parentNode.NodeType == TreeNodeType.Arc ? parentNode.Id : parentNode.ArcId, |
| | 1 | 123 | | Type = articleType, |
| | 1 | 124 | | EffectiveDate = DateTime.Now |
| | 1 | 125 | | }; |
| | | 126 | | |
| | 1 | 127 | | var created = await _articleApi.CreateArticleAsync(createDto); |
| | 1 | 128 | | if (created == null) |
| | | 129 | | { |
| | 0 | 130 | | _logger.LogWarning("Failed to create child article under {ParentId}", parentId); |
| | 0 | 131 | | return null; |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | // Refresh tree |
| | 1 | 135 | | if (_refreshCallback != null) |
| | | 136 | | { |
| | 1 | 137 | | await _refreshCallback(); |
| | | 138 | | } |
| | | 139 | | |
| | 1 | 140 | | return created.Id; |
| | 2 | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Determines the appropriate article type for a child based on its parent node. |
| | | 145 | | /// </summary> |
| | | 146 | | private static ArticleType DetermineChildArticleType(TreeNode parentNode) |
| | | 147 | | { |
| | 1 | 148 | | return parentNode.NodeType switch |
| | 1 | 149 | | { |
| | 0 | 150 | | TreeNodeType.VirtualGroup => parentNode.VirtualGroupType switch |
| | 0 | 151 | | { |
| | 0 | 152 | | VirtualGroupType.Wiki => ArticleType.WikiArticle, |
| | 0 | 153 | | VirtualGroupType.PlayerCharacters => ArticleType.Character, |
| | 0 | 154 | | VirtualGroupType.Uncategorized => ArticleType.Legacy, |
| | 0 | 155 | | _ => ArticleType.WikiArticle |
| | 0 | 156 | | }, |
| | 1 | 157 | | TreeNodeType.Arc => ArticleType.Session, |
| | 0 | 158 | | TreeNodeType.Article => parentNode.ArticleType ?? ArticleType.WikiArticle, |
| | 0 | 159 | | _ => ArticleType.WikiArticle |
| | 1 | 160 | | }; |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | // ============================================ |
| | | 164 | | // Delete Operations |
| | | 165 | | // ============================================ |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Deletes an article and all its descendants. |
| | | 169 | | /// </summary> |
| | | 170 | | /// <param name="articleId">The article ID to delete.</param> |
| | | 171 | | /// <returns>True if deletion succeeded.</returns> |
| | | 172 | | public async Task<bool> DeleteArticleAsync(Guid articleId) |
| | | 173 | | { |
| | 3 | 174 | | if (!_nodeIndex.TryGetNode(articleId, out var node) || node == null) |
| | | 175 | | { |
| | 1 | 176 | | return false; |
| | | 177 | | } |
| | | 178 | | |
| | 2 | 179 | | if (node.NodeType != TreeNodeType.Article) |
| | | 180 | | { |
| | 1 | 181 | | _logger.LogWarning("Cannot delete non-article node {NodeId}", articleId); |
| | 1 | 182 | | return false; |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | try |
| | | 186 | | { |
| | 1 | 187 | | await _articleApi.DeleteArticleAsync(articleId); |
| | | 188 | | |
| | | 189 | | // Refresh tree |
| | 1 | 190 | | if (_refreshCallback != null) |
| | | 191 | | { |
| | 1 | 192 | | await _refreshCallback(); |
| | | 193 | | } |
| | | 194 | | |
| | 1 | 195 | | return true; |
| | | 196 | | } |
| | 0 | 197 | | catch (Exception ex) |
| | | 198 | | { |
| | 0 | 199 | | _logger.LogError(ex, "Failed to delete article {ArticleId}", articleId); |
| | 0 | 200 | | return false; |
| | | 201 | | } |
| | 3 | 202 | | } |
| | | 203 | | |
| | | 204 | | // ============================================ |
| | | 205 | | // Move Operations |
| | | 206 | | // ============================================ |
| | | 207 | | |
| | | 208 | | /// <summary> |
| | | 209 | | /// Moves an article to a new parent (or to root if newParentId is null). |
| | | 210 | | /// </summary> |
| | | 211 | | /// <param name="articleId">The article to move.</param> |
| | | 212 | | /// <param name="newParentId">The new parent ID, or null for root level.</param> |
| | | 213 | | /// <returns>True if move succeeded.</returns> |
| | | 214 | | public async Task<bool> MoveArticleAsync(Guid articleId, Guid? newParentId) |
| | | 215 | | { |
| | 5 | 216 | | if (!_nodeIndex.TryGetNode(articleId, out var node) || node == null) |
| | | 217 | | { |
| | 0 | 218 | | return false; |
| | | 219 | | } |
| | | 220 | | |
| | 5 | 221 | | if (node.NodeType != TreeNodeType.Article) |
| | | 222 | | { |
| | 0 | 223 | | _logger.LogWarning("Cannot move non-article node"); |
| | 0 | 224 | | return false; |
| | | 225 | | } |
| | | 226 | | |
| | | 227 | | // Check if target is a virtual group |
| | 5 | 228 | | TreeNode? targetNode = null; |
| | 5 | 229 | | if (newParentId.HasValue) |
| | | 230 | | { |
| | 5 | 231 | | _nodeIndex.TryGetNode(newParentId.Value, out targetNode); |
| | | 232 | | } |
| | | 233 | | |
| | | 234 | | // Handle drop onto virtual group specially |
| | 5 | 235 | | if (targetNode?.NodeType == TreeNodeType.VirtualGroup) |
| | | 236 | | { |
| | 2 | 237 | | return await MoveToVirtualGroupAsync(articleId, node, targetNode); |
| | | 238 | | } |
| | | 239 | | |
| | | 240 | | // Regular move to another article or root |
| | 3 | 241 | | return await MoveToArticleOrRootAsync(articleId, node, newParentId, targetNode); |
| | 5 | 242 | | } |
| | | 243 | | |
| | | 244 | | /// <summary> |
| | | 245 | | /// Handles moving an article to a virtual group. |
| | | 246 | | /// </summary> |
| | | 247 | | private async Task<bool> MoveToVirtualGroupAsync(Guid articleId, TreeNode node, TreeNode targetNode) |
| | | 248 | | { |
| | | 249 | | // Campaigns group holds Campaign entities, not articles |
| | 2 | 250 | | if (targetNode.VirtualGroupType == VirtualGroupType.Campaigns) |
| | | 251 | | { |
| | 1 | 252 | | _logger.LogWarning("Cannot drop articles into Campaigns group - campaigns are separate entities"); |
| | 1 | 253 | | return false; |
| | | 254 | | } |
| | | 255 | | |
| | | 256 | | // Links group holds external links, not articles |
| | 1 | 257 | | if (targetNode.VirtualGroupType == VirtualGroupType.Links) |
| | | 258 | | { |
| | 0 | 259 | | _logger.LogWarning("Cannot drop articles into Links group - links are separate entities"); |
| | 0 | 260 | | return false; |
| | | 261 | | } |
| | | 262 | | |
| | | 263 | | // Determine new article type based on target group |
| | 1 | 264 | | var newType = targetNode.VirtualGroupType switch |
| | 1 | 265 | | { |
| | 1 | 266 | | VirtualGroupType.Wiki => ArticleType.WikiArticle, |
| | 0 | 267 | | VirtualGroupType.PlayerCharacters => ArticleType.Character, |
| | 0 | 268 | | VirtualGroupType.Uncategorized => ArticleType.Legacy, |
| | 0 | 269 | | _ => node.ArticleType ?? ArticleType.WikiArticle |
| | 1 | 270 | | }; |
| | | 271 | | |
| | | 272 | | try |
| | | 273 | | { |
| | | 274 | | // First, move to root level (null parent) |
| | 1 | 275 | | var moveSuccess = await _articleApi.MoveArticleAsync(articleId, null); |
| | 1 | 276 | | if (!moveSuccess) |
| | | 277 | | { |
| | 0 | 278 | | _logger.LogWarning("Failed to move article to root level"); |
| | 0 | 279 | | return false; |
| | | 280 | | } |
| | | 281 | | |
| | | 282 | | // Then update the type if it changed |
| | 1 | 283 | | if (node.ArticleType != newType) |
| | | 284 | | { |
| | 1 | 285 | | var fullArticle = await _articleApi.GetArticleDetailAsync(articleId); |
| | 1 | 286 | | if (fullArticle != null) |
| | | 287 | | { |
| | 1 | 288 | | var updateDto = new ArticleUpdateDto |
| | 1 | 289 | | { |
| | 1 | 290 | | Title = fullArticle.Title, |
| | 1 | 291 | | Body = fullArticle.Body, |
| | 1 | 292 | | Type = newType, |
| | 1 | 293 | | IconEmoji = fullArticle.IconEmoji, |
| | 1 | 294 | | EffectiveDate = fullArticle.EffectiveDate |
| | 1 | 295 | | }; |
| | | 296 | | |
| | 1 | 297 | | var updated = await _articleApi.UpdateArticleAsync(articleId, updateDto); |
| | 1 | 298 | | if (updated == null) |
| | | 299 | | { |
| | 0 | 300 | | _logger.LogWarning("Failed to update article type"); |
| | | 301 | | // Move succeeded but type update failed - still refresh |
| | | 302 | | } |
| | | 303 | | } |
| | | 304 | | } |
| | | 305 | | |
| | 1 | 306 | | if (_refreshCallback != null) |
| | | 307 | | { |
| | 1 | 308 | | await _refreshCallback(); |
| | | 309 | | } |
| | | 310 | | |
| | 1 | 311 | | return true; |
| | | 312 | | } |
| | 0 | 313 | | catch (Exception ex) |
| | | 314 | | { |
| | 0 | 315 | | _logger.LogError(ex, "Failed to move article to virtual group"); |
| | 0 | 316 | | return false; |
| | | 317 | | } |
| | 2 | 318 | | } |
| | | 319 | | |
| | | 320 | | /// <summary> |
| | | 321 | | /// Handles moving an article to another article or to root level. |
| | | 322 | | /// </summary> |
| | | 323 | | private async Task<bool> MoveToArticleOrRootAsync(Guid articleId, TreeNode node, Guid? newParentId, TreeNode? target |
| | | 324 | | { |
| | | 325 | | // Prevent moving to self or descendant |
| | 3 | 326 | | if (newParentId.HasValue) |
| | | 327 | | { |
| | 3 | 328 | | if (newParentId.Value == articleId) |
| | | 329 | | { |
| | 1 | 330 | | _logger.LogWarning("Cannot move article to itself"); |
| | 1 | 331 | | return false; |
| | | 332 | | } |
| | | 333 | | |
| | 2 | 334 | | if (IsDescendantOf(newParentId.Value, articleId)) |
| | | 335 | | { |
| | 1 | 336 | | _logger.LogWarning("Cannot move article to its descendant"); |
| | 1 | 337 | | return false; |
| | | 338 | | } |
| | | 339 | | |
| | | 340 | | // Ensure target is an article (not a World, Campaign, or Arc) |
| | 1 | 341 | | if (targetNode != null && targetNode.NodeType != TreeNodeType.Article) |
| | | 342 | | { |
| | 0 | 343 | | _logger.LogWarning("Cannot move article to non-article node type: {NodeType}", targetNode.NodeType); |
| | 0 | 344 | | return false; |
| | | 345 | | } |
| | | 346 | | } |
| | | 347 | | |
| | | 348 | | try |
| | | 349 | | { |
| | 1 | 350 | | var success = await _articleApi.MoveArticleAsync(articleId, newParentId); |
| | | 351 | | |
| | 1 | 352 | | if (success && _refreshCallback != null) |
| | | 353 | | { |
| | 1 | 354 | | await _refreshCallback(); |
| | | 355 | | } |
| | | 356 | | |
| | 1 | 357 | | return success; |
| | | 358 | | } |
| | 0 | 359 | | catch (Exception ex) |
| | | 360 | | { |
| | 0 | 361 | | _logger.LogError(ex, "Failed to move article {ArticleId}", articleId); |
| | 0 | 362 | | return false; |
| | | 363 | | } |
| | 3 | 364 | | } |
| | | 365 | | |
| | | 366 | | /// <summary> |
| | | 367 | | /// Checks if a node is a descendant of a potential ancestor. |
| | | 368 | | /// </summary> |
| | | 369 | | public bool IsDescendantOf(Guid nodeId, Guid potentialAncestorId) |
| | | 370 | | { |
| | 6 | 371 | | if (!_nodeIndex.TryGetNode(nodeId, out var node) || node == null) |
| | 0 | 372 | | return false; |
| | | 373 | | |
| | 6 | 374 | | var current = node; |
| | 7 | 375 | | while (current.ParentId.HasValue) |
| | | 376 | | { |
| | 4 | 377 | | if (current.ParentId.Value == potentialAncestorId) |
| | 3 | 378 | | return true; |
| | | 379 | | |
| | 1 | 380 | | if (!_nodeIndex.TryGetNode(current.ParentId.Value, out var parent) || parent == null) |
| | | 381 | | break; |
| | | 382 | | |
| | 1 | 383 | | current = parent; |
| | | 384 | | } |
| | | 385 | | |
| | 3 | 386 | | return false; |
| | | 387 | | } |
| | | 388 | | |
| | | 389 | | // ============================================ |
| | | 390 | | // Update Operations |
| | | 391 | | // ============================================ |
| | | 392 | | |
| | | 393 | | /// <summary> |
| | | 394 | | /// Updates a node's display properties (title, icon) after an article is saved. |
| | | 395 | | /// This is a local-only update; does not make API calls. |
| | | 396 | | /// </summary> |
| | | 397 | | /// <returns>True if the node was found and updated.</returns> |
| | | 398 | | public bool UpdateNodeDisplay(Guid nodeId, string title, string? iconEmoji) |
| | | 399 | | { |
| | 2 | 400 | | if (_nodeIndex.TryGetNode(nodeId, out var node) && node != null) |
| | | 401 | | { |
| | 1 | 402 | | node.Title = title; |
| | 1 | 403 | | node.IconEmoji = iconEmoji; |
| | 1 | 404 | | return true; |
| | | 405 | | } |
| | 1 | 406 | | return false; |
| | | 407 | | } |
| | | 408 | | |
| | | 409 | | /// <summary> |
| | | 410 | | /// Updates a node's visibility after privacy is toggled. |
| | | 411 | | /// This is a local-only update; does not make API calls. |
| | | 412 | | /// </summary> |
| | | 413 | | /// <returns>True if the node was found and updated.</returns> |
| | | 414 | | public bool UpdateNodeVisibility(Guid nodeId, ArticleVisibility visibility) |
| | | 415 | | { |
| | 2 | 416 | | if (_nodeIndex.TryGetNode(nodeId, out var node) && node != null) |
| | | 417 | | { |
| | 1 | 418 | | node.Visibility = visibility; |
| | 1 | 419 | | return true; |
| | | 420 | | } |
| | 1 | 421 | | return false; |
| | | 422 | | } |
| | | 423 | | |
| | | 424 | | // ============================================ |
| | | 425 | | // Validation Helpers |
| | | 426 | | // ============================================ |
| | | 427 | | |
| | | 428 | | /// <summary> |
| | | 429 | | /// Checks if a node exists and is an article. |
| | | 430 | | /// </summary> |
| | | 431 | | public bool IsValidArticle(Guid nodeId) |
| | | 432 | | { |
| | 3 | 433 | | return _nodeIndex.TryGetNode(nodeId, out var node) && |
| | 3 | 434 | | node != null && |
| | 3 | 435 | | node.NodeType == TreeNodeType.Article; |
| | | 436 | | } |
| | | 437 | | |
| | | 438 | | /// <summary> |
| | | 439 | | /// Checks if a node can accept children. |
| | | 440 | | /// </summary> |
| | | 441 | | public bool CanAcceptChildren(Guid nodeId) |
| | | 442 | | { |
| | 2 | 443 | | if (!_nodeIndex.TryGetNode(nodeId, out var node) || node == null) |
| | 0 | 444 | | return false; |
| | | 445 | | |
| | 2 | 446 | | return node.CanAddChildren; |
| | | 447 | | } |
| | | 448 | | |
| | | 449 | | /// <summary> |
| | | 450 | | /// Checks if a node can be a drop target for drag-and-drop. |
| | | 451 | | /// </summary> |
| | | 452 | | public bool IsValidDropTarget(Guid nodeId) |
| | | 453 | | { |
| | 2 | 454 | | if (!_nodeIndex.TryGetNode(nodeId, out var node) || node == null) |
| | 0 | 455 | | return false; |
| | | 456 | | |
| | 2 | 457 | | return node.IsDropTarget; |
| | | 458 | | } |
| | | 459 | | } |