using DocumentFormat.OpenXml.InkML;
using Indotalent.Data.Entities;
using Indotalent.Infrastructure.Authentication.Identity;
using Indotalent.Infrastructure.Authorization.Identity;
using Microsoft.AspNetCore.Identity;

namespace Indotalent.Infrastructure.Database.Demo;

public static class UserSeeder
{
    public static async Task GenerateDataAsync(UserManager<ApplicationUser> userManager, AppDbContext context, IConfiguration configuration)
    {

        var adminSettings = configuration.GetSection("IdentitySettings:DefaultAdmin").Get<DefaultAdminSettings>();
        if (adminSettings == null) return;

        if (context.Users.Where(x => x.Email != adminSettings.Email).Any()) return;

        var userNames = new List<string>
        {
            "Alex", "Taylor", "Jordan", "Morgan", "Riley",
            "Casey", "Peyton", "Cameron", "Jamie", "Drew",
            "Dakota", "Avery", "Quinn", "Harper", "Rowan",
            "Emerson", "Finley", "Skyler", "Charlie", "Sage"
        };

        var defaultPassword = "123456";
        var domain = "@example.com";
        var role = ApplicationRoles.Member;

        foreach (var name in userNames)
        {
            var email = $"{name.ToLower()}{domain}";

            if (await userManager.FindByEmailAsync(email) == null)
            {
                var applicationUser = new ApplicationUser
                {
                    UserName = email,
                    Email = email,
                    FullName = name,
                    EmailConfirmed = true
                };

                var result = await userManager.CreateAsync(applicationUser, defaultPassword);

                if (result.Succeeded)
                {
                    await userManager.AddToRoleAsync(applicationUser, role);
                }
            }
        }
    }
}