| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | using Spectre.Console.Cli; |
| | | 3 | | using Spectre.Console; |
| | | 4 | | using System.Text.Json; |
| | | 5 | | using EHonda.KicktippAi.Core; |
| | | 6 | | using Orchestrator.Infrastructure.Factories; |
| | | 7 | | |
| | | 8 | | namespace Orchestrator.Commands.Utility.UploadKpi; |
| | | 9 | | |
| | | 10 | | public class UploadKpiCommand : AsyncCommand<UploadKpiSettings> |
| | | 11 | | { |
| | | 12 | | private readonly IAnsiConsole _console; |
| | | 13 | | private readonly IFirebaseServiceFactory _firebaseServiceFactory; |
| | | 14 | | private readonly ILogger<UploadKpiCommand> _logger; |
| | | 15 | | |
| | 0 | 16 | | public UploadKpiCommand( |
| | 0 | 17 | | IAnsiConsole console, |
| | 0 | 18 | | IFirebaseServiceFactory firebaseServiceFactory, |
| | 0 | 19 | | ILogger<UploadKpiCommand> logger) |
| | | 20 | | { |
| | 0 | 21 | | _console = console; |
| | 0 | 22 | | _firebaseServiceFactory = firebaseServiceFactory; |
| | 0 | 23 | | _logger = logger; |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | public override async Task<int> ExecuteAsync(CommandContext context, UploadKpiSettings settings) |
| | | 27 | | { |
| | | 28 | | |
| | | 29 | | try |
| | | 30 | | { |
| | 0 | 31 | | _console.MarkupLine($"[green]Upload KPI command initialized for document:[/] [yellow]{settings.DocumentName} |
| | 0 | 32 | | _console.MarkupLine($"[blue]Using community context:[/] [yellow]{settings.CommunityContext}[/]"); |
| | | 33 | | |
| | 0 | 34 | | if (settings.Verbose) |
| | | 35 | | { |
| | 0 | 36 | | _console.MarkupLine("[dim]Verbose mode enabled[/]"); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | // Check if the JSON file exists in the community-context specific subfolder |
| | 0 | 40 | | var jsonFilePath = Path.Combine("kpi-documents", "output", settings.CommunityContext, $"{settings.DocumentNa |
| | 0 | 41 | | if (!File.Exists(jsonFilePath)) |
| | | 42 | | { |
| | 0 | 43 | | _console.MarkupLine($"[red]KPI document file not found:[/] {jsonFilePath}"); |
| | 0 | 44 | | _console.MarkupLine($"[dim]Run the PowerShell script with firebase mode to create the document first.[/] |
| | 0 | 45 | | return 1; |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | _console.MarkupLine($"[blue]Reading KPI document from:[/] {jsonFilePath}"); |
| | | 49 | | |
| | | 50 | | // Read and parse the JSON file |
| | 0 | 51 | | var jsonContent = await File.ReadAllTextAsync(jsonFilePath); |
| | 0 | 52 | | var kpiDocument = JsonSerializer.Deserialize<KpiDocumentJson>(jsonContent, new JsonSerializerOptions |
| | 0 | 53 | | { |
| | 0 | 54 | | PropertyNameCaseInsensitive = true |
| | 0 | 55 | | }); |
| | | 56 | | |
| | 0 | 57 | | if (kpiDocument == null) |
| | | 58 | | { |
| | 0 | 59 | | _console.MarkupLine("[red]Failed to parse KPI document JSON[/]"); |
| | 0 | 60 | | return 1; |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | if (settings.Verbose) |
| | | 64 | | { |
| | 0 | 65 | | _console.MarkupLine($"[dim]Document Name: {kpiDocument.DocumentName}[/]"); |
| | 0 | 66 | | _console.MarkupLine($"[dim]Community Context: {kpiDocument.CommunityContext}[/]"); |
| | 0 | 67 | | _console.MarkupLine($"[dim]Content length: {kpiDocument.Content.Length} characters[/]"); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | // Create Firebase services using factory (factory handles env var loading) |
| | 0 | 71 | | var kpiRepository = _firebaseServiceFactory.CreateKpiRepository(); |
| | | 72 | | |
| | | 73 | | // Check if document already exists for this community context |
| | 0 | 74 | | var existingDocument = await kpiRepository.GetKpiDocumentAsync(kpiDocument.DocumentName, kpiDocument.Communi |
| | | 75 | | |
| | 0 | 76 | | if (existingDocument != null) |
| | | 77 | | { |
| | 0 | 78 | | _console.MarkupLine($"[blue]Found existing KPI document '{kpiDocument.DocumentName}' (version {existingD |
| | 0 | 79 | | _console.MarkupLine($"[blue]Checking for content changes...[/]"); |
| | | 80 | | |
| | 0 | 81 | | if (settings.Verbose) |
| | | 82 | | { |
| | 0 | 83 | | _console.MarkupLine($"[dim]Current content length: {existingDocument.Content.Length} characters[/]") |
| | 0 | 84 | | _console.MarkupLine($"[dim]New content length: {kpiDocument.Content.Length} characters[/]"); |
| | | 85 | | } |
| | | 86 | | } |
| | | 87 | | else |
| | | 88 | | { |
| | 0 | 89 | | _console.MarkupLine($"[blue]No existing KPI document found for '{kpiDocument.DocumentName}' - will creat |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | // Upload the document (versioning is handled automatically by the repository) |
| | 0 | 93 | | _console.MarkupLine($"[blue]Processing KPI document...[/]"); |
| | | 94 | | |
| | 0 | 95 | | var savedVersion = await kpiRepository.SaveKpiDocumentAsync( |
| | 0 | 96 | | kpiDocument.DocumentName, |
| | 0 | 97 | | kpiDocument.Content, |
| | 0 | 98 | | kpiDocument.Description, |
| | 0 | 99 | | kpiDocument.CommunityContext); |
| | | 100 | | |
| | 0 | 101 | | if (existingDocument != null && savedVersion == existingDocument.Version) |
| | | 102 | | { |
| | 0 | 103 | | _console.MarkupLine($"[green]✓ Content unchanged - KPI document '[/][white]{kpiDocument.DocumentName}[/] |
| | | 104 | | } |
| | 0 | 105 | | else if (existingDocument != null) |
| | | 106 | | { |
| | 0 | 107 | | _console.MarkupLine($"[green]✓ Content changed - Created new version {savedVersion} for KPI document '[/ |
| | | 108 | | } |
| | | 109 | | else |
| | | 110 | | { |
| | 0 | 111 | | _console.MarkupLine($"[green]✓ Successfully created KPI document '[/][white]{kpiDocument.DocumentName}[/ |
| | | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | if (settings.Verbose) |
| | | 115 | | { |
| | 0 | 116 | | _console.MarkupLine($"[dim]Document saved to unified kpi-documents collection with community context: {k |
| | 0 | 117 | | _console.MarkupLine($"[dim]Document version: {savedVersion}[/]"); |
| | | 118 | | } |
| | | 119 | | |
| | 0 | 120 | | return 0; |
| | | 121 | | } |
| | 0 | 122 | | catch (Exception ex) |
| | | 123 | | { |
| | 0 | 124 | | _logger.LogError(ex, "Error in upload-kpi command"); |
| | 0 | 125 | | _console.MarkupLine($"[red]Error: {ex.Message}[/]"); |
| | 0 | 126 | | return 1; |
| | | 127 | | } |
| | 0 | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// JSON model for deserializing KPI document files. |
| | | 132 | | /// </summary> |
| | | 133 | | private class KpiDocumentJson |
| | | 134 | | { |
| | 0 | 135 | | public string DocumentName { get; set; } = string.Empty; |
| | 0 | 136 | | public string Content { get; set; } = string.Empty; |
| | 0 | 137 | | public string Description { get; set; } = string.Empty; |
| | 0 | 138 | | public string CommunityContext { get; set; } = string.Empty; |
| | | 139 | | } |
| | | 140 | | } |