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

namespace Indotalent.Infrastructure.Database.Demo;

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

        var random = new Random();
        var entityName = nameof(LeadContact);
        var dateFinish = DateTime.Now;
        var dateStart = new DateTime(dateFinish.AddMonths(-11).Year, dateFinish.AddMonths(-11).Month, 1);

        var leads = await context.Lead.Select(l => l.Id).ToListAsync();

        if (!leads.Any()) return;

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

            foreach (var contactDate in contactDates)
            {
                var leadId = GetRandomValue(leads, random);

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

                var leadContact = new LeadContact
                {
                    LeadId = leadId,
                    AutoNumber = autoNo,
                    FullName = $"Contact {random.Next(1000, 9999)}",
                    Description = "Sample contact description",
                    AddressStreet = "456 Elm St",
                    AddressCity = "Anytown",
                    AddressState = "State",
                    AddressZipCode = "67890",
                    AddressCountry = "Country",
                    PhoneNumber = $"+1{random.Next(100, 999)}-{random.Next(100, 999)}-{random.Next(1000, 9999)}",
                    FaxNumber = $"+1{random.Next(100, 999)}-{random.Next(100, 999)}-{random.Next(1000, 9999)}",
                    MobileNumber = $"+1{random.Next(100, 999)}-{random.Next(100, 999)}-{random.Next(1000, 9999)}",
                    Email = $"contact{random.Next(1000, 9999)}@company.com",
                    Website = $"www.contact{random.Next(1000, 9999)}.com",
                    WhatsApp = $"+1{random.Next(100, 999)}-{random.Next(100, 999)}-{random.Next(1000, 9999)}",
                    LinkedIn = $"linkedin.com/in/contact{random.Next(1000, 9999)}",
                    Facebook = $"facebook.com/contact{random.Next(1000, 9999)}",
                    Twitter = $"twitter.com/contact{random.Next(1000, 9999)}",
                    Instagram = $"instagram.com/contact{random.Next(1000, 9999)}",
                    AvatarName = $"avatar_{random.Next(1, 100)}.jpg"
                };

                await context.LeadContact.AddAsync(leadContact);
            }
        }

        await context.SaveChangesAsync();
    }

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

    private static DateTime[] GetRandomDays(int year, int month, int count)
    {
        var random = new Random();
        int daysInMonthCount = DateTime.DaysInMonth(year, month);
        var daysInMonth = Enumerable.Range(1, daysInMonthCount).ToList();
        var selectedDays = new List<int>();

        for (int i = 0; i < count && daysInMonth.Count > 0; i++)
        {
            int day = daysInMonth[random.Next(daysInMonth.Count)];
            selectedDays.Add(day);
            daysInMonth.Remove(day);
        }

        return selectedDays.Select(day => new DateTime(year, month, day)).ToArray();
    }
}