using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using SVSim.Bootstrap.Models.Seed;
using SVSim.Database;
using SVSim.Database.Models;
namespace SVSim.Bootstrap.Importers;
///
/// Idempotent upsert of battle-pass level rows from seeds/battle-pass-levels.json.
/// Per-level reward_data blob preserved verbatim (shape varies per level).
///
public class BattlePassImporter
{
public async Task ImportAsync(SVSimDbContext context, string seedDir)
{
var seed = SeedLoader.LoadList(Path.Combine(seedDir, "battle-pass-levels.json"));
if (seed.Count == 0) return 0;
var existing = await context.BattlePassLevels.ToDictionaryAsync(e => e.Id);
int created = 0, updated = 0;
foreach (var s in seed)
{
if (s.Id == 0) continue;
var entry = existing.TryGetValue(s.Id, out var ex) ? ex : new BattlePassLevelEntry { Id = s.Id };
entry.RewardData = s.RewardData.ValueKind == JsonValueKind.Undefined
? "{}"
: JsonSerializer.Serialize(s.RewardData);
if (ex is null) { context.BattlePassLevels.Add(entry); existing[s.Id] = entry; created++; }
else updated++;
}
await context.SaveChangesAsync();
Console.WriteLine($"[BattlePassImporter] +{created}/~{updated}");
return created + updated;
}
}