| | | 1 | | using EHonda.KicktippAi.Core; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using Spectre.Console.Cli; |
| | | 4 | | using Spectre.Console; |
| | | 5 | | using Orchestrator.Infrastructure; |
| | | 6 | | using Orchestrator.Infrastructure.Factories; |
| | | 7 | | |
| | | 8 | | namespace Orchestrator.Commands.Utility.ListKpi; |
| | | 9 | | |
| | | 10 | | public class ListKpiCommand : AsyncCommand<ListKpiSettings> |
| | | 11 | | { |
| | | 12 | | private readonly IAnsiConsole _console; |
| | | 13 | | private readonly IFirebaseServiceFactory _firebaseServiceFactory; |
| | | 14 | | private readonly ILogger<ListKpiCommand> _logger; |
| | | 15 | | |
| | 1 | 16 | | public ListKpiCommand( |
| | 1 | 17 | | IAnsiConsole console, |
| | 1 | 18 | | IFirebaseServiceFactory firebaseServiceFactory, |
| | 1 | 19 | | ILogger<ListKpiCommand> logger) |
| | | 20 | | { |
| | 1 | 21 | | _console = console; |
| | 1 | 22 | | _firebaseServiceFactory = firebaseServiceFactory; |
| | 1 | 23 | | _logger = logger; |
| | 1 | 24 | | } |
| | | 25 | | |
| | | 26 | | protected override async Task<int> ExecuteAsync(CommandContext context, ListKpiSettings settings, CancellationToken |
| | | 27 | | { |
| | | 28 | | |
| | | 29 | | try |
| | | 30 | | { |
| | 1 | 31 | | _console.MarkupLine($"[green]List KPI command initialized for community context:[/] [yellow]{settings.Commun |
| | 1 | 32 | | var competition = CompetitionResolver.ResolveCompetition(settings.Competition, settings.CommunityContext, se |
| | 1 | 33 | | var repositoryCompetition = CompetitionResolver.ToRepositoryCompetitionArgument(competition); |
| | 1 | 34 | | _console.MarkupLine($"[blue]Using competition:[/] [yellow]{competition}[/]"); |
| | | 35 | | |
| | 1 | 36 | | if (settings.Verbose) |
| | | 37 | | { |
| | 1 | 38 | | _console.MarkupLine("[dim]Verbose mode enabled[/]"); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | // Create Firebase services using factory (factory handles env var loading) |
| | 1 | 42 | | var kpiRepository = _firebaseServiceFactory.CreateKpiRepository(repositoryCompetition); |
| | | 43 | | |
| | 1 | 44 | | var table = new Table(); |
| | 1 | 45 | | table.AddColumn("Document Name"); |
| | 1 | 46 | | table.AddColumn("Version"); |
| | 1 | 47 | | table.AddColumn("Content Preview"); |
| | 1 | 48 | | table.AddColumn("Description"); |
| | | 49 | | |
| | 1 | 50 | | int documentCount = 0; |
| | | 51 | | |
| | | 52 | | // Get all latest documents directly from repository for better version support |
| | 1 | 53 | | var kpiDocuments = await kpiRepository.GetAllKpiDocumentsAsync(settings.CommunityContext); |
| | | 54 | | |
| | 1 | 55 | | foreach (var document in kpiDocuments) |
| | | 56 | | { |
| | 1 | 57 | | var preview = document.Content.Length > 100 |
| | 1 | 58 | | ? document.Content.Substring(0, 100) + "..." |
| | 1 | 59 | | : document.Content; |
| | | 60 | | |
| | 1 | 61 | | var description = document.Description.Length > 50 |
| | 1 | 62 | | ? document.Description.Substring(0, 50) + "..." |
| | 1 | 63 | | : document.Description; |
| | | 64 | | |
| | 1 | 65 | | table.AddRow( |
| | 1 | 66 | | $"[yellow]{document.DocumentName}[/]", |
| | 1 | 67 | | $"[blue]v{document.Version}[/]", |
| | 1 | 68 | | $"[dim]{preview.Replace("\n", " ").Replace("\t", " ")}[/]", |
| | 1 | 69 | | $"[dim]{description}[/]"); |
| | | 70 | | |
| | 1 | 71 | | documentCount++; |
| | | 72 | | } |
| | | 73 | | |
| | 1 | 74 | | _console.Write(table); |
| | 1 | 75 | | _console.MarkupLine($"[green]Found {documentCount} KPI document(s)[/]"); |
| | | 76 | | |
| | 1 | 77 | | return 0; |
| | | 78 | | } |
| | 1 | 79 | | catch (Exception ex) |
| | | 80 | | { |
| | 1 | 81 | | _logger.LogError(ex, "Error in list-kpi command"); |
| | 1 | 82 | | _console.MarkupLine($"[red]Error: {ex.Message}[/]"); |
| | 1 | 83 | | return 1; |
| | | 84 | | } |
| | 1 | 85 | | } |
| | | 86 | | } |