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

namespace Indotalent.Infrastructure.Database.Demo;

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

        var random = new Random();
        var scrappingStatusValues = Enum.GetValues(typeof(ScrappingStatus)).Cast<ScrappingStatus>().ToList();
        var entityName = nameof(Scrapping);
        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 scrapping = new Scrapping
                {
                    AutoNumber = autoNo,
                    ScrappingDate = transDate,
                    Status = scrappingStatusValues[random.Next(scrappingStatusValues.Count)],
                    WarehouseId = GetRandomValue(warehouses, random),
                };
                await context.Scrapping.AddAsync(scrapping);

                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 = scrapping.Id,
                        ModuleName = entityName,
                        ModuleCode = "SCRP",
                        ModuleNumber = scrapping.AutoNumber,
                        MovementDate = scrapping.ScrappingDate!.Value,
                        Status = (InventoryTransactionStatus)scrapping.Status,
                        AutoNumber = autoNoIVT,
                        WarehouseId = scrapping.WarehouseId,
                        ProductId = product.Id,
                        Movement = (double)random.Next(1, 10)
                    };

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