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