using Microsoft.EntityFrameworkCore;
using SVSim.Bootstrap.Models.Seed;
using SVSim.Database;
using SVSim.Database.Models;
namespace SVSim.Bootstrap.Importers;
///
/// Idempotent upsert of the item catalog from seeds/items.json. Source is the client's
/// item_master.csv + itemtext.json (extracted via
/// data_dumps/scripts/extract-items.py). Rows missing from the seed are LEFT INTACT.
///
public class ItemImporter
{
public async Task ImportAsync(SVSimDbContext context, string seedDir)
{
string path = Path.Combine(seedDir, "items.json");
var seed = SeedLoader.LoadList(path);
if (seed.Count == 0)
{
Console.WriteLine("[ItemImporter] No seed rows; skipping.");
return 0;
}
var existing = await context.Items.ToDictionaryAsync(e => e.Id);
int created = 0, updated = 0;
foreach (var s in seed)
{
if (s.ItemId == 0) continue;
var entry = existing.TryGetValue(s.ItemId, out var ex)
? ex : new ItemEntry { Id = s.ItemId };
entry.Name = s.Name;
entry.Type = s.Type;
entry.ThumbnailPath = s.ThumbnailPath;
if (ex is null)
{
context.Items.Add(entry);
existing[s.ItemId] = entry;
created++;
}
else updated++;
}
await context.SaveChangesAsync();
Console.WriteLine($"[ItemImporter] +{created}/~{updated}");
return created + updated;
}
}