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