| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | using Spectre.Console.Cli; |
| | | 3 | | using Spectre.Console; |
| | | 4 | | using EHonda.KicktippAi.Core; |
| | | 5 | | using Orchestrator.Infrastructure.Factories; |
| | | 6 | | |
| | | 7 | | namespace Orchestrator.Commands.Operations.CollectContext; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Command for collecting Kicktipp context documents and storing them in the database. |
| | | 11 | | /// </summary> |
| | | 12 | | public class CollectContextKicktippCommand : AsyncCommand<CollectContextKicktippSettings> |
| | | 13 | | { |
| | | 14 | | private readonly IAnsiConsole _console; |
| | | 15 | | private readonly IFirebaseServiceFactory _firebaseServiceFactory; |
| | | 16 | | private readonly IKicktippClientFactory _kicktippClientFactory; |
| | | 17 | | private readonly IContextProviderFactory _contextProviderFactory; |
| | | 18 | | private readonly ILogger<CollectContextKicktippCommand> _logger; |
| | | 19 | | |
| | 1 | 20 | | public CollectContextKicktippCommand( |
| | 1 | 21 | | IAnsiConsole console, |
| | 1 | 22 | | IFirebaseServiceFactory firebaseServiceFactory, |
| | 1 | 23 | | IKicktippClientFactory kicktippClientFactory, |
| | 1 | 24 | | IContextProviderFactory contextProviderFactory, |
| | 1 | 25 | | ILogger<CollectContextKicktippCommand> logger) |
| | | 26 | | { |
| | 1 | 27 | | _console = console; |
| | 1 | 28 | | _firebaseServiceFactory = firebaseServiceFactory; |
| | 1 | 29 | | _kicktippClientFactory = kicktippClientFactory; |
| | 1 | 30 | | _contextProviderFactory = contextProviderFactory; |
| | 1 | 31 | | _logger = logger; |
| | 1 | 32 | | } |
| | | 33 | | |
| | | 34 | | public override async Task<int> ExecuteAsync(CommandContext context, CollectContextKicktippSettings settings) |
| | | 35 | | { |
| | | 36 | | |
| | | 37 | | try |
| | | 38 | | { |
| | | 39 | | // Validate settings |
| | 1 | 40 | | if (string.IsNullOrWhiteSpace(settings.CommunityContext)) |
| | | 41 | | { |
| | 1 | 42 | | _console.MarkupLine("[red]Error: Community context is required[/]"); |
| | 1 | 43 | | return 1; |
| | | 44 | | } |
| | | 45 | | |
| | 1 | 46 | | _console.MarkupLine($"[green]Collect-context kicktipp command initialized[/]"); |
| | | 47 | | |
| | 1 | 48 | | if (settings.Verbose) |
| | | 49 | | { |
| | 1 | 50 | | _console.MarkupLine("[dim]Verbose mode enabled[/]"); |
| | | 51 | | } |
| | | 52 | | |
| | 1 | 53 | | if (settings.DryRun) |
| | | 54 | | { |
| | 1 | 55 | | _console.MarkupLine("[magenta]Dry run mode enabled - no changes will be made to database[/]"); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | // Execute the context collection workflow |
| | 1 | 59 | | await ExecuteKicktippContextCollection(settings); |
| | | 60 | | |
| | 1 | 61 | | return 0; |
| | | 62 | | } |
| | 1 | 63 | | catch (Exception ex) |
| | | 64 | | { |
| | 1 | 65 | | _logger.LogError(ex, "Error executing collect-context kicktipp command"); |
| | 1 | 66 | | _console.MarkupLine($"[red]Error:[/] {ex.Message}"); |
| | 1 | 67 | | return 1; |
| | | 68 | | } |
| | 1 | 69 | | } |
| | | 70 | | |
| | | 71 | | private async Task ExecuteKicktippContextCollection(CollectContextKicktippSettings settings) |
| | | 72 | | { |
| | | 73 | | // Create services using factories (factories handle env var loading) |
| | 1 | 74 | | var kicktippClient = _kicktippClientFactory.CreateClient(); |
| | 1 | 75 | | var contextRepository = _firebaseServiceFactory.CreateContextRepository(); |
| | | 76 | | |
| | | 77 | | // Create context provider using factory |
| | 1 | 78 | | var contextProvider = _contextProviderFactory.CreateKicktippContextProvider( |
| | 1 | 79 | | kicktippClient, settings.CommunityContext, settings.CommunityContext); |
| | | 80 | | |
| | 1 | 81 | | _console.MarkupLine($"[blue]Using community context:[/] [yellow]{settings.CommunityContext}[/]"); |
| | 1 | 82 | | _console.MarkupLine("[blue]Getting current matchday matches...[/]"); |
| | | 83 | | |
| | | 84 | | // Step 1: Get current matchday matches |
| | 1 | 85 | | var matchesWithHistory = await kicktippClient.GetMatchesWithHistoryAsync(settings.CommunityContext); |
| | | 86 | | |
| | 1 | 87 | | if (!matchesWithHistory.Any()) |
| | | 88 | | { |
| | 1 | 89 | | _console.MarkupLine("[yellow]No matches found for current matchday[/]"); |
| | 1 | 90 | | return; |
| | | 91 | | } |
| | | 92 | | |
| | 1 | 93 | | _console.MarkupLine($"[green]Found {matchesWithHistory.Count} matches for current matchday[/]"); |
| | | 94 | | |
| | | 95 | | // Step 2: Collect all unique context documents for all matches |
| | 1 | 96 | | var allContextDocuments = new Dictionary<string, string>(); // documentName -> content |
| | | 97 | | |
| | 1 | 98 | | foreach (var matchWithHistory in matchesWithHistory) |
| | | 99 | | { |
| | 1 | 100 | | var match = matchWithHistory.Match; |
| | 1 | 101 | | _console.MarkupLine($"[cyan]Collecting context for:[/] {match.HomeTeam} vs {match.AwayTeam}"); |
| | | 102 | | |
| | | 103 | | try |
| | | 104 | | { |
| | | 105 | | // Get context for this specific match |
| | 1 | 106 | | await foreach (var contextDoc in contextProvider.GetMatchContextAsync(match.HomeTeam, match.AwayTeam)) |
| | | 107 | | { |
| | | 108 | | // Use the document name as key to avoid duplicates |
| | 1 | 109 | | if (!allContextDocuments.ContainsKey(contextDoc.Name)) |
| | | 110 | | { |
| | 1 | 111 | | allContextDocuments[contextDoc.Name] = contextDoc.Content; |
| | | 112 | | |
| | 1 | 113 | | if (settings.Verbose) |
| | | 114 | | { |
| | 1 | 115 | | _console.MarkupLine($"[dim] Collected context document: {contextDoc.Name}[/]"); |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | } |
| | 1 | 119 | | } |
| | 1 | 120 | | catch (Exception ex) |
| | | 121 | | { |
| | 1 | 122 | | _logger.LogError(ex, "Failed to collect context for match {HomeTeam} vs {AwayTeam}", match.HomeTeam, mat |
| | 1 | 123 | | _console.MarkupLine($"[red] ✗ Failed to collect context: {ex.Message}[/]"); |
| | 1 | 124 | | } |
| | 1 | 125 | | } |
| | | 126 | | |
| | 1 | 127 | | _console.MarkupLine($"[green]Collected {allContextDocuments.Count} unique context documents[/]"); |
| | | 128 | | |
| | | 129 | | // Step 3: Save context documents to database |
| | 1 | 130 | | var savedCount = 0; |
| | 1 | 131 | | var skippedCount = 0; |
| | 1 | 132 | | var currentDate = DateTime.Now.ToString("yyyy-MM-dd"); |
| | | 133 | | |
| | 1 | 134 | | foreach (var (documentName, content) in allContextDocuments) |
| | | 135 | | { |
| | | 136 | | try |
| | | 137 | | { |
| | 1 | 138 | | if (settings.DryRun) |
| | | 139 | | { |
| | 1 | 140 | | _console.MarkupLine($"[magenta] Dry run - would save:[/] {documentName}"); |
| | 1 | 141 | | continue; |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | // Check if this is a history document that needs Data_Collected_At column |
| | 1 | 145 | | string finalContent = content; |
| | 1 | 146 | | if (IsHistoryDocument(documentName)) |
| | | 147 | | { |
| | | 148 | | // Get the previous version to compare against |
| | 1 | 149 | | var previousDocument = await contextRepository.GetLatestContextDocumentAsync(documentName, settings. |
| | 1 | 150 | | var previousContent = previousDocument?.Content; |
| | | 151 | | |
| | | 152 | | // Add Data_Collected_At column with current date for new matches |
| | 1 | 153 | | finalContent = HistoryCsvUtility.AddDataCollectedAtColumn(content, previousContent, currentDate); |
| | | 154 | | |
| | 1 | 155 | | if (settings.Verbose) |
| | | 156 | | { |
| | 1 | 157 | | _console.MarkupLine($"[dim] Added Data_Collected_At column to {documentName}[/]"); |
| | | 158 | | } |
| | | 159 | | } |
| | | 160 | | |
| | 1 | 161 | | var savedVersion = await contextRepository.SaveContextDocumentAsync( |
| | 1 | 162 | | documentName, |
| | 1 | 163 | | finalContent, |
| | 1 | 164 | | settings.CommunityContext); |
| | | 165 | | |
| | 1 | 166 | | if (savedVersion.HasValue) |
| | | 167 | | { |
| | 1 | 168 | | savedCount++; |
| | 1 | 169 | | if (settings.Verbose) |
| | | 170 | | { |
| | 1 | 171 | | _console.MarkupLine($"[green] ✓ Saved {documentName} as version {savedVersion.Value}[/]"); |
| | | 172 | | } |
| | | 173 | | } |
| | | 174 | | else |
| | | 175 | | { |
| | 1 | 176 | | skippedCount++; |
| | 1 | 177 | | if (settings.Verbose) |
| | | 178 | | { |
| | 1 | 179 | | _console.MarkupLine($"[dim] - Skipped {documentName} (content unchanged)[/]"); |
| | | 180 | | } |
| | | 181 | | } |
| | 1 | 182 | | } |
| | 1 | 183 | | catch (Exception ex) |
| | | 184 | | { |
| | 1 | 185 | | _logger.LogError(ex, "Failed to save context document {DocumentName}", documentName); |
| | 1 | 186 | | _console.MarkupLine($"[red] ✗ Failed to save {documentName}: {ex.Message}[/]"); |
| | 1 | 187 | | } |
| | 1 | 188 | | } |
| | | 189 | | |
| | 1 | 190 | | if (settings.DryRun) |
| | | 191 | | { |
| | 1 | 192 | | _console.MarkupLine($"[magenta]✓ Dry run completed - would have processed {allContextDocuments.Count} docume |
| | | 193 | | } |
| | | 194 | | else |
| | | 195 | | { |
| | 1 | 196 | | _console.MarkupLine($"[green]✓ Context collection completed![/]"); |
| | 1 | 197 | | _console.MarkupLine($"[green] Saved: {savedCount} documents[/]"); |
| | 1 | 198 | | _console.MarkupLine($"[dim] Skipped: {skippedCount} documents (unchanged)[/]"); |
| | | 199 | | } |
| | 1 | 200 | | } |
| | | 201 | | |
| | | 202 | | private static bool IsHistoryDocument(string documentName) |
| | | 203 | | { |
| | 1 | 204 | | return documentName.StartsWith("recent-history-", StringComparison.OrdinalIgnoreCase) || |
| | 1 | 205 | | documentName.StartsWith("home-history-", StringComparison.OrdinalIgnoreCase) || |
| | 1 | 206 | | documentName.StartsWith("away-history-", StringComparison.OrdinalIgnoreCase); |
| | | 207 | | } |
| | | 208 | | } |