| | | 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.UploadTransfers; |
| | | 12 | | |
| | | 13 | | public class UploadTransfersCommand : AsyncCommand<UploadTransfersSettings> |
| | | 14 | | { |
| | | 15 | | private readonly IAnsiConsole _console; |
| | | 16 | | private readonly IFirebaseServiceFactory _firebaseServiceFactory; |
| | | 17 | | private readonly IFileProvider _fileProvider; |
| | | 18 | | private readonly ILogger<UploadTransfersCommand> _logger; |
| | | 19 | | |
| | | 20 | | public UploadTransfersCommand( |
| | | 21 | | IAnsiConsole console, |
| | | 22 | | IFirebaseServiceFactory firebaseServiceFactory, |
| | | 23 | | [FromKeyedServices(ServiceRegistrationExtensions.TransfersDocumentsFileProviderKey)] IFileProvider fileProvider, |
| | | 24 | | ILogger<UploadTransfersCommand> logger) |
| | | 25 | | { |
| | | 26 | | _console = console; |
| | | 27 | | _firebaseServiceFactory = firebaseServiceFactory; |
| | | 28 | | _fileProvider = fileProvider; |
| | | 29 | | _logger = logger; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | public override async Task<int> ExecuteAsync(CommandContext context, UploadTransfersSettings settings) |
| | | 33 | | { |
| | | 34 | | |
| | | 35 | | try |
| | | 36 | | { |
| | | 37 | | var docName = $"{settings.TeamAbbreviation.ToLowerInvariant()}-transfers.csv"; |
| | | 38 | | _console.MarkupLine($"[green]Upload Transfers command initialized for document:[/] [yellow]{docName}[/]"); |
| | | 39 | | _console.MarkupLine($"[blue]Using community context:[/] [yellow]{settings.CommunityContext}[/]"); |
| | | 40 | | if (settings.Verbose) _console.MarkupLine("[dim]Verbose mode enabled[/]"); |
| | | 41 | | |
| | | 42 | | // JSON file path produced by Create-TransfersDocument.ps1 firebase mode |
| | | 43 | | var jsonPath = $"output/{settings.CommunityContext}/{docName}.json"; |
| | | 44 | | var fileInfo = _fileProvider.GetFileInfo(jsonPath); |
| | | 45 | | if (!fileInfo.Exists) |
| | | 46 | | { |
| | | 47 | | _console.MarkupLine($"[red]Transfers document JSON not found:[/] {jsonPath}"); |
| | | 48 | | _console.MarkupLine("[dim]Run Create-TransfersDocument.ps1 in firebase mode first.[/]"); |
| | | 49 | | return 1; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | _console.MarkupLine($"[blue]Reading transfers document from:[/] {jsonPath}"); |
| | | 53 | | using var stream = fileInfo.CreateReadStream(); |
| | | 54 | | var transfersDoc = await JsonSerializer.DeserializeAsync<TransfersDocumentJson>(stream, new JsonSerializerOp |
| | | 55 | | if (transfersDoc == null) |
| | | 56 | | { |
| | | 57 | | _console.MarkupLine("[red]Failed to parse transfers document JSON[/]"); |
| | | 58 | | return 1; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | if (settings.Verbose) |
| | | 62 | | { |
| | | 63 | | _console.MarkupLine($"[dim]Document Name: {transfersDoc.DocumentName}[/]"); |
| | | 64 | | _console.MarkupLine($"[dim]Community Context: {transfersDoc.CommunityContext}[/]"); |
| | | 65 | | _console.MarkupLine($"[dim]Content length: {transfersDoc.Content.Length} characters[/]"); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | // Create Firebase services using factory (factory handles env var loading) |
| | | 69 | | var contextRepo = _firebaseServiceFactory.CreateContextRepository(); |
| | | 70 | | var existing = await contextRepo.GetLatestContextDocumentAsync(transfersDoc.DocumentName, transfersDoc.Commu |
| | | 71 | | if (existing != null) |
| | | 72 | | { |
| | | 73 | | _console.MarkupLine($"[blue]Found existing transfers document '{transfersDoc.DocumentName}' (version {ex |
| | | 74 | | if (settings.Verbose) |
| | | 75 | | { |
| | | 76 | | _console.MarkupLine("[dim]Checking for changes...[/]"); |
| | | 77 | | } |
| | | 78 | | } |
| | | 79 | | else |
| | | 80 | | { |
| | | 81 | | _console.MarkupLine($"[blue]No existing transfers document found - will create version 0[/]"); |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | var savedVersion = await contextRepo.SaveContextDocumentAsync( |
| | | 85 | | transfersDoc.DocumentName, |
| | | 86 | | transfersDoc.Content, |
| | | 87 | | transfersDoc.CommunityContext); |
| | | 88 | | |
| | | 89 | | if (existing != null && savedVersion == null) |
| | | 90 | | { |
| | | 91 | | _console.MarkupLine($"[green]✓ Content unchanged - transfers document remains at version {existing.Versi |
| | | 92 | | } |
| | | 93 | | else if (existing != null) |
| | | 94 | | { |
| | | 95 | | _console.MarkupLine($"[green]✓ Content changed - created new version {savedVersion}[/]"); |
| | | 96 | | } |
| | | 97 | | else |
| | | 98 | | { |
| | | 99 | | _console.MarkupLine($"[green]✓ Created transfers document version {savedVersion}[/]"); |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | return 0; |
| | | 103 | | } |
| | | 104 | | catch (Exception ex) |
| | | 105 | | { |
| | | 106 | | _logger.LogError(ex, "Error in upload-transfers command"); |
| | | 107 | | _console.MarkupLine($"[red]Error: {ex.Message}[/]"); |
| | | 108 | | return 1; |
| | | 109 | | } |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | private class TransfersDocumentJson |
| | | 113 | | { |
| | 1 | 114 | | public string DocumentName { get; set; } = string.Empty; |
| | 1 | 115 | | public string Content { get; set; } = string.Empty; |
| | 1 | 116 | | public string Description { get; set; } = string.Empty; |
| | 1 | 117 | | public string CommunityContext { get; set; } = string.Empty; |
| | | 118 | | } |
| | | 119 | | } |