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

namespace Indotalent.Infrastructure.Database.Demo;

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

        var random = new Random();
        var goodsReceiveStatusValues = Enum.GetValues(typeof(GoodsReceiveStatus)).Cast<GoodsReceiveStatus>().ToList();
        var grEntityName = nameof(GoodsReceive);
        var ivtEntityName = nameof(InventoryTransaction);

        var purchaseOrders = await context.PurchaseOrder
            .Where(x => x.OrderStatus >= PurchaseOrderStatus.Confirmed)
            .ToListAsync();

        var warehouses = await context.Warehouse
            .Where(x => x.SystemWarehouse == false)
            .Select(x => x.Id)
            .ToListAsync();

        if (!warehouses.Any()) return;

        foreach (var purchaseOrder in purchaseOrders)
        {
            var autoNoGR = await context.GenerateAutoNumberAsync(
                entityName: grEntityName,
                prefixTemplate: $"{grEntityName.ToShortNameConsonant(3)}/{{Year}}/"
            );

            var goodsReceive = new GoodsReceive
            {
                AutoNumber = autoNoGR,
                ReceiveDate = purchaseOrder.OrderDate?.AddDays(random.Next(1, 5)),
                Status = goodsReceiveStatusValues[random.Next(goodsReceiveStatusValues.Count)],
                PurchaseOrderId = purchaseOrder.Id,
            };
            await context.GoodsReceive.AddAsync(goodsReceive);

            var items = await context.PurchaseOrderItem
                .Include(x => x.Product)
                .Where(x => x.PurchaseOrderId == purchaseOrder.Id && x.Product!.Physical == true)
                .ToListAsync();

            foreach (var item in items)
            {
                var autoNoIVT = await context.GenerateAutoNumberAsync(
                    entityName: ivtEntityName,
                    prefixTemplate: $"{ivtEntityName.ToShortNameConsonant(3)}/{{Year}}/"
                );

                var inventoryTransaction = new InventoryTransaction
                {
                    ModuleId = goodsReceive.Id,
                    ModuleName = grEntityName,
                    ModuleCode = "GR",
                    ModuleNumber = goodsReceive.AutoNumber,
                    MovementDate = goodsReceive.ReceiveDate!.Value,
                    Status = (InventoryTransactionStatus)goodsReceive.Status,
                    AutoNumber = autoNoIVT,
                    WarehouseId = GetRandomValue(warehouses, random),
                    ProductId = item.ProductId,
                    Movement = item.Quantity!.Value
                };

                context.CalculateInvenTrans(inventoryTransaction);

                await context.InventoryTransaction.AddAsync(inventoryTransaction);
            }

            await context.SaveChangesAsync();
        }
    }

    private static T GetRandomValue<T>(List<T> list, Random random)
    {
        return list[random.Next(list.Count)];
    }
}