namespace SOR.Ai.Seeding; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using SOR.Ai.Core; using SOR.Database.Contexts; // SorIdentityDbContext /// /// One-shot IHostedService that runs at startup before the server accepts traffic. /// Seeds AI accounts and decks, then loads the AiAccountRegistry so other services /// can check IsAi without hitting the database on every turn. /// internal sealed class AiSeedStartupService : IHostedService { private readonly IServiceScopeFactory _scopeFactory; private readonly AiAccountRegistry _registry; private readonly ILogger _logger; public AiSeedStartupService( IServiceScopeFactory scopeFactory, AiAccountRegistry registry, ILogger logger) { _scopeFactory = scopeFactory; _registry = registry; _logger = logger; } public async Task StartAsync(CancellationToken cancellationToken) { await using var scope = _scopeFactory.CreateAsyncScope(); var sp = scope.ServiceProvider; var identityDb = sp.GetRequiredService(); // 1. Seed AI accounts (idempotent). await AiAccountSeeder.SeedAsync(identityDb, _logger, cancellationToken); // 2. Populate in-memory registry for O(1) IsAi checks. // Decks are no longer pre-seeded — AiDeckResolver picks a prebuilt deck // per-game based on admin-configured AiDeckAssignments. _registry.Load(identityDb); _logger.LogInformation( "SOR.Ai: startup seed complete. {Count} AI accounts registered.", _registry.Count); } public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; }