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

namespace Indotalent.Infrastructure.Database.Demo;

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

        var random = new Random();
        var entityName = nameof(SalesRepresentative);
        var salesTeams = await context.SalesTeam.ToListAsync();

        foreach (var team in salesTeams)
        {
            for (int i = 1; i <= 5; i++)
            {
                var autoNo = await context.GenerateAutoNumberAsync(
                    entityName: entityName,
                    prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/"
                );

                var salesRep = new SalesRepresentative
                {
                    Name = $"Rep {i} - {team.Name}",
                    AutoNumber = autoNo,
                    JobTitle = "Sales " + (i == 1 ? "Manager" : "Representative"),
                    EmployeeNumber = $"EMP-{random.Next(1000, 9999)}",
                    PhoneNumber = $"+1{random.Next(100, 999)}-{random.Next(100, 999)}-{random.Next(1000, 9999)}",
                    EmailAddress = $"salesrep{i}@company.com",
                    Description = $"Sales Rep for {team.Name}",
                    SalesTeamId = team.Id
                };

                await context.SalesRepresentative.AddAsync(salesRep);
            }
        }

        await context.SaveChangesAsync();
    }
}