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