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

namespace Indotalent.Infrastructure.Database.Demo;

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

        var groups = await context.VendorGroup.Select(x => x.Id).ToArrayAsync();
        var categories = await context.VendorCategory.Select(x => x.Id).ToArrayAsync();

        var cities = new string[] { "New York", "Los Angeles", "San Francisco", "Chicago" };
        var streets = new string[] { "Main Street", "Broadway", "Elm Street", "Maple Avenue" };
        var states = new string[] { "NY", "CA", "IL", "TX" };
        var zipCodes = new string[] { "10001", "90001", "60601", "73301" };
        var phoneNumbers = new string[] { "123-456-7890", "987-654-3210", "555-123-4567", "111-222-3333" };
        var emails = new string[] { "vendor1@example.com", "vendor2@example.com", "vendor3@example.com", "vendor4@example.com" };

        var random = new Random();
        var entityName = nameof(Vendor);

        var vendors = new List<Vendor>
        {
            new Vendor { Name = "Quantum Industries" },
            new Vendor { Name = "Apex Ventures" },
            new Vendor { Name = "Horizon Enterprises" },
            new Vendor { Name = "Nova Innovations" },
            new Vendor { Name = "Phoenix Holdings" },
            new Vendor { Name = "Titan Group" },
            new Vendor { Name = "Zenith Corporation" },
            new Vendor { Name = "Prime Solutions" },
            new Vendor { Name = "Cascade Enterprises" },
            new Vendor { Name = "Aurora Holdings" },
            new Vendor { Name = "Vanguard Industries" },
            new Vendor { Name = "Empyrean Ventures" },
            new Vendor { Name = "Genesis Corporation" },
            new Vendor { Name = "Equinox Enterprises" },
            new Vendor { Name = "Summit Holdings" },
            new Vendor { Name = "Sovereign Solutions" },
            new Vendor { Name = "Spectrum Corporation" },
            new Vendor { Name = "Elysium Enterprises" },
            new Vendor { Name = "Infinity Holdings" },
            new Vendor { Name = "Momentum Ventures" }
        };

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

            vendor.AutoNumber = autoNo;
            vendor.VendorGroupId = GetRandomValue(groups, random);
            vendor.VendorCategoryId = GetRandomValue(categories, random);
            vendor.City = GetRandomString(cities, random);
            vendor.Street = GetRandomString(streets, random);
            vendor.State = GetRandomString(states, random);
            vendor.ZipCode = GetRandomString(zipCodes, random);
            vendor.PhoneNumber = GetRandomString(phoneNumbers, random);
            vendor.EmailAddress = GetRandomString(emails, random);

            await context.Vendor.AddAsync(vendor);
        }

        await context.SaveChangesAsync();
    }

    private static T GetRandomValue<T>(T[] array, Random random)
    {
        return array[random.Next(array.Length)];
    }

    private static string GetRandomString(string[] array, Random random)
    {
        return array[random.Next(array.Length)];
    }
}