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

namespace Indotalent.Infrastructure.Database.Demo;

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

        var random = new Random();
        var deliveryOrderStatusValues = Enum.GetValues(typeof(DeliveryOrderStatus)).Cast<DeliveryOrderStatus>().ToList();
        var doEntityName = nameof(DeliveryOrder);
        var ivtEntityName = nameof(InventoryTransaction);

        var salesOrders = await context.SalesOrder
            .Where(x => x.OrderStatus >= SalesOrderStatus.Confirmed)
            .ToListAsync();

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

        if (!warehouses.Any()) return;

        foreach (var salesOrder in salesOrders)
        {
            var autoNoDO = await context.GenerateAutoNumberAsync(
                entityName: doEntityName,
                prefixTemplate: $"{doEntityName.ToShortNameConsonant(3)}/{{Year}}/"
            );

            var deliveryOrder = new DeliveryOrder
            {
                AutoNumber = autoNoDO,
                DeliveryDate = salesOrder.OrderDate?.AddDays(random.Next(1, 5)),
                Status = deliveryOrderStatusValues[random.Next(deliveryOrderStatusValues.Count)],
                SalesOrderId = salesOrder.Id,
            };
            await context.DeliveryOrder.AddAsync(deliveryOrder);

            var items = await context.SalesOrderItem
                .Include(x => x.Product)
                .Where(x => x.SalesOrderId == salesOrder.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 = deliveryOrder.Id,
                    ModuleName = doEntityName,
                    ModuleCode = "DO",
                    ModuleNumber = deliveryOrder.AutoNumber,
                    MovementDate = deliveryOrder.DeliveryDate!.Value,
                    Status = (InventoryTransactionStatus)deliveryOrder.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)];
    }
}