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

namespace Indotalent.Infrastructure.Database.Demo;

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

        var random = new Random();
        var adjustmentStatusValues = Enum.GetValues(typeof(AdjustmentStatus)).Cast<AdjustmentStatus>().ToList();
        var entityName = nameof(NegativeAdjustment);
        var ivtEntityName = nameof(InventoryTransaction);

        var products = await context.Product
            .Where(x => x.Physical == true)
            .ToListAsync();

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

        if (!products.Any() || !warehouses.Any()) return;

        var dateFinish = DateTime.Now;
        var dateStart = new DateTime(dateFinish.AddMonths(-12).Year, dateFinish.AddMonths(-12).Month, 1);

        for (DateTime date = dateStart; date < dateFinish; date = date.AddMonths(1))
        {
            DateTime[] transactionDates = GetRandomDays(date.Year, date.Month, 6);

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

                var adjustmentMinus = new NegativeAdjustment
                {
                    AutoNumber = autoNo,
                    AdjustmentDate = transDate,
                    Status = adjustmentStatusValues[random.Next(adjustmentStatusValues.Count)],
                };
                await context.NegativeAdjustment.AddAsync(adjustmentMinus);

                int numberOfProducts = random.Next(3, 6);
                for (int i = 0; i < numberOfProducts; i++)
                {
                    var product = products[random.Next(products.Count)];

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

                    var inventoryTransaction = new InventoryTransaction
                    {
                        ModuleId = adjustmentMinus.Id,
                        ModuleName = entityName,
                        ModuleCode = "ADJ-",
                        ModuleNumber = adjustmentMinus.AutoNumber,
                        MovementDate = adjustmentMinus.AdjustmentDate!.Value,
                        Status = (InventoryTransactionStatus)adjustmentMinus.Status,
                        AutoNumber = autoNoIVT,
                        WarehouseId = GetRandomValue(warehouses, random),
                        ProductId = product.Id,
                        Movement = random.Next(1, 3)
                    };

                    context.CalculateInvenTrans(inventoryTransaction);
                    await context.InventoryTransaction.AddAsync(inventoryTransaction);
                }
            }
        }

        await context.SaveChangesAsync();
    }

    private static DateTime[] GetRandomDays(int year, int month, int count)
    {
        var random = new Random();
        var daysInMonth = DateTime.DaysInMonth(year, month);
        return Enumerable.Range(1, count)
            .Select(_ => new DateTime(year, month, random.Next(1, daysInMonth + 1)))
            .ToArray();
    }

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