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

namespace Indotalent.Infrastructure.Database.Demo;

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

        var firstNames = new string[]
        {
            "Adam", "Sarah", "Michael", "Emily", "David", "Jessica",
            "Kevin", "Samantha", "Jason", "Olivia", "Matthew", "Ashley",
            "Christopher", "Jennifer", "Nicholas", "Amanda", "Alexander",
            "Stephanie", "Jonathan", "Lauren"
        };

        var lastNames = new string[]
        {
            "Johnson", "Williams", "Brown", "Jones", "Miller", "Davis",
            "Garcia", "Rodriguez", "Wilson", "Martinez", "Anderson", "Taylor",
            "Thomas", "Hernandez", "Moore", "Martin", "Jackson", "Thompson",
            "White", "Lopez"
        };

        var jobTitles = new string[]
        {
            "Chief Executive Officer", "Data Scientist", "Product Manager", "Business Development Executive",
            "IT Consultant", "Social Media Specialist", "Research Analyst", "Content Writer",
            "Operations Manager", "Financial Planner", "Software Developer", "Customer Success Manager",
            "Marketing Coordinator", "Quality Assurance Tester", "HR Specialist", "Event Coordinator",
            "Account Executive", "Network Administrator", "Sales Manager", "Legal Assistant"
        };

        var customerIds = await context.Customer.Select(x => x.Id).ToListAsync();
        var random = new Random();
        var entityName = nameof(CustomerContact);

        foreach (var customerId in customerIds)
        {
            for (int i = 0; i < 3; i++)
            {
                var firstName = GetRandomString(firstNames, random);
                var lastName = GetRandomString(lastNames, random);

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

                var contact = new CustomerContact
                {
                    Name = $"{firstName} {lastName}",
                    AutoNumber = autoNo,
                    CustomerId = customerId,
                    JobTitle = GetRandomString(jobTitles, random),
                    EmailAddress = $"{firstName.ToLower()}.{lastName.ToLower()}@gmail.com",
                    PhoneNumber = GenerateRandomPhoneNumber(random)
                };

                await context.CustomerContact.AddAsync(contact);
            }
        }

        await context.SaveChangesAsync();
    }

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

    private static string GenerateRandomPhoneNumber(Random random)
    {
        return $"+1-{random.Next(100, 999)}-{random.Next(100, 999)}-{random.Next(1000, 9999)}";
    }
}