using System.Text.Json;
using Meezi.Infrastructure.Data;

/// <summary>
/// One-time importer: copies Food-101 JPEGs into uploads/{cafeId}/ and updates menu-image-manifest paths.
/// Usage: dotnet run --project tools/MenuImageImporter -- --food101 "C:\data\food-101\images" --cafe cafe_demo_001 --out uploads
/// </summary>
var argsList = args.ToList();
string? food101Root = GetArg("--food101");
var cafeId = GetArg("--cafe") ?? "cafe_demo_001";
var outRoot = GetArg("--out") ?? "uploads";
var manifestPath = GetArg("--manifest")
    ?? Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..", "..", "data", "menu-image-manifest.json"));

var defaultClassMap = DemoMenuCatalog.Items
    .ToDictionary(i => i.Id, i => i.Food101Class, StringComparer.Ordinal);

if (string.IsNullOrEmpty(food101Root) || !Directory.Exists(food101Root))
{
    Console.WriteLine("Food-101 image root not found. Pass --food101 <path-to-food-101/images>.");
    Console.WriteLine("Mapping (demo item id → Food-101 class folder):");
    foreach (var (id, cls) in defaultClassMap)
        Console.WriteLine($"  {id} → {cls}");
    return 1;
}

if (!File.Exists(manifestPath))
{
    Console.WriteLine($"Manifest not found: {manifestPath}");
    return 1;
}

var manifestJson = await File.ReadAllTextAsync(manifestPath);
using var doc = JsonDocument.Parse(manifestJson);
var root = doc.RootElement.Clone();
var items = root.GetProperty("items");

var cafeDir = Path.Combine(outRoot, cafeId);
Directory.CreateDirectory(cafeDir);

var updated = 0;
foreach (var (itemId, className) in defaultClassMap)
{
    var classDir = Path.Combine(food101Root, className);
    if (!Directory.Exists(classDir))
    {
        Console.WriteLine($"Skip {itemId}: class folder missing {className}");
        continue;
    }

    var source = Directory.GetFiles(classDir, "*.jpg").Concat(Directory.GetFiles(classDir, "*.jpeg")).FirstOrDefault();
    if (source is null) continue;

    var destName = $"{itemId}.jpg";
    var destPath = Path.Combine(cafeDir, destName);
    File.Copy(source, destPath, overwrite: true);

    if (items.TryGetProperty(itemId, out var entry) && entry.ValueKind == JsonValueKind.Object)
    {
        var rel = $"/uploads/{cafeId}/{destName}";
        Console.WriteLine($"Copied {itemId} → {destPath} (set imageUrl: {rel})");
        updated++;
    }
}

Console.WriteLine($"Done. {updated} images copied to {cafeDir}. Re-run API seeder or PATCH menu items with /uploads paths.");
return 0;

string? GetArg(string name)
{
    var i = argsList.IndexOf(name);
    return i >= 0 && i + 1 < argsList.Count ? argsList[i + 1] : null;
}
