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

namespace Indotalent.Infrastructure.Database.Demo;

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

        var random = new Random();
        var purchaseReturnStatusValues = Enum.GetValues(typeof(PurchaseReturnStatus)).Cast<PurchaseReturnStatus>().ToList();
        var prnEntityName = nameof(PurchaseReturn);
        var grEntityName = nameof(GoodsReceive);
        var ivtEntityName = nameof(InventoryTransaction);

        var goodsReceives = await context.GoodsReceive
            .Where(x => x.Status >= GoodsReceiveStatus.Confirmed)
            .ToListAsync();

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

        if (!warehouses.Any()) return;

        foreach (var goodsReceive in goodsReceives)
        {
            bool skip = random.Next(2) == 0;
            if (skip) continue;

            var autoNoPRN = await context.GenerateAutoNumberAsync(
                entityName: prnEntityName,
                prefixTemplate: $"{prnEntityName.ToShortNameConsonant(3)}/{{Year}}/"
            );

            var purchaseReturn = new PurchaseReturn
            {
                AutoNumber = autoNoPRN,
                ReturnDate = goodsReceive.ReceiveDate?.AddDays(random.Next(1, 5)),
                Status = purchaseReturnStatusValues[random.Next(purchaseReturnStatusValues.Count)],
                GoodsReceiveId = goodsReceive.Id,
            };
            await context.PurchaseReturn.AddAsync(purchaseReturn);

            var items = await context.InventoryTransaction
                .Where(x => x.ModuleId == goodsReceive.Id && x.ModuleName == grEntityName)
                .ToListAsync();

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

                var inventoryTransaction = new InventoryTransaction
                {
                    ModuleId = purchaseReturn.Id,
                    ModuleName = prnEntityName,
                    ModuleCode = "PRN",
                    ModuleNumber = purchaseReturn.AutoNumber,
                    MovementDate = purchaseReturn.ReturnDate!.Value,
                    Status = (InventoryTransactionStatus)purchaseReturn.Status,
                    AutoNumber = autoNoIVT,
                    WarehouseId = GetRandomValue(warehouses, random),
                    ProductId = item.ProductId,
                    Movement = item.Movement
                };

                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)];
    }
}