using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;

namespace SpaceGame.Api.Auth.Simulation;

public sealed class DevAuthSeeder(
    IHostEnvironment hostEnvironment,
    IOptions<AuthOptions> authOptions,
    IAuthRepository authRepository,
    LocalPasswordHasher passwordHasher)
{
    public async Task SeedAsync(CancellationToken cancellationToken)
    {
        if (!hostEnvironment.IsDevelopment())
        {
            return;
        }

        foreach (var seedUser in authOptions.Value.DevSeedUsers)
        {
            if (string.IsNullOrWhiteSpace(seedUser.Email) || string.IsNullOrWhiteSpace(seedUser.Password))
            {
                continue;
            }

            await authRepository.UpsertUserAsync(
              seedUser.Email.Trim().ToLowerInvariant(),
              passwordHasher.HashPassword(seedUser.Password),
              seedUser.Roles,
              cancellationToken);
        }
    }
}
