using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Data.Entities;
using Microsoft.EntityFrameworkCore;

namespace Indotalent.Infrastructure.Database.Demo;

public static class ProductSeeder
{
    public static async Task GenerateDataAsync(AppDbContext context)
    {
        if (await context.Product.AnyAsync()) return;

        var productGroups = await context.ProductGroup.ToListAsync();
        var measureId = await context.UnitMeasure
            .Where(x => x.Name == "unit")
            .Select(x => x.Id)
            .FirstOrDefaultAsync();

        if (string.IsNullOrEmpty(measureId)) return;

        var groupMapping = productGroups
            .Where(pg => !string.IsNullOrEmpty(pg.Name) && !string.IsNullOrEmpty(pg.Id))
            .ToDictionary(pg => pg.Name!, pg => pg.Id!);

        var entityName = nameof(Product);
        var products = new List<Product>
        {
            // Hardware - Gunakan akhiran 'm' untuk decimal literal
            new Product { Name = "Dell Servers", UnitPrice = 5000m, ProductGroupId = groupMapping["Hardware"] },
            new Product { Name = "Dell Desktop Computers", UnitPrice = 2000m, ProductGroupId = groupMapping["Hardware"] },
            new Product { Name = "Dell Laptops", UnitPrice = 3000m, ProductGroupId = groupMapping["Hardware"] },

            // Networking
            new Product { Name = "Network Cables", UnitPrice = 100m, ProductGroupId = groupMapping["Networking"] },
            new Product { Name = "Routers and Switches", UnitPrice = 1000m, ProductGroupId = groupMapping["Networking"] },
            new Product { Name = "Antennas and Signal Boosters", UnitPrice = 2000m, ProductGroupId = groupMapping["Networking"] },
            new Product { Name = "Wifii", UnitPrice = 1000m, ProductGroupId = groupMapping["Networking"] },

            // Storage
            new Product { Name = "HDD 500", UnitPrice = 500m, ProductGroupId = groupMapping["Storage"] },
            new Product { Name = "HDD 1T", UnitPrice = 800m, ProductGroupId = groupMapping["Storage"] },
            new Product { Name = "SSD 500", UnitPrice = 1000m, ProductGroupId = groupMapping["Storage"] },
            new Product { Name = "SSD 1T", UnitPrice = 1500m, ProductGroupId = groupMapping["Storage"] },

            // Device
            new Product { Name = "Dell Keyboard", UnitPrice = 700m, ProductGroupId = groupMapping["Device"] },
            new Product { Name = "Dell Mouse", UnitPrice = 500m, ProductGroupId = groupMapping["Device"] },
            new Product { Name = "Dell Monitor 27inch", UnitPrice = 1000m, ProductGroupId = groupMapping["Device"] },
            new Product { Name = "Dell Monitor 32inch", UnitPrice = 1500m, ProductGroupId = groupMapping["Device"] },
            new Product { Name = "Dell Webcams", UnitPrice = 500m, ProductGroupId = groupMapping["Device"] },

            // Software
            new Product { Name = "D365 License", UnitPrice = 800m, Physical = false, ProductGroupId = groupMapping["Software"] },

            // Service
            new Product { Name = "IT Security", UnitPrice = 500m, Physical = false, ProductGroupId = groupMapping["Service"] },
            new Product { Name = "Discount", UnitPrice = -10m, Physical = false, ProductGroupId = groupMapping["Service"] }
        };

        foreach (var product in products)
        {
            var autoNo = await context.GenerateAutoNumberAsync(
                entityName: entityName,
                prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/"
            );

            product.AutoNumber = autoNo;
            product.UnitMeasureId = measureId;
            product.Physical ??= true;

            await context.Product.AddAsync(product);
        }

        await context.SaveChangesAsync();
    }
}