using Microsoft.EntityFrameworkCore;

public static class UserSeeder
{
    public static readonly Guid SimulationRunnerUserId =
        Guid.Parse("55555555-5555-5555-5555-555555555555");

    public static async Task SeedAsync(
        AppDbContext db,
        bool simulationEnabled = false,
        string? simulationRunnerPassword = null)
    {
        if (!await db.ClinicalUsers.AnyAsync())
        {
            db.ClinicalUsers.AddRange(
                new ClinicalUser
                {
                    Id = Guid.Parse("11111111-1111-1111-1111-111111111111"),
                    Username = "nurse.demo",
                    PasswordHash = BCrypt.Net.BCrypt.HashPassword("DemoNurse1!"),
                    DisplayName = "Demo Nurse",
                    Role = ClinicalRole.Nurse,
                    CreatedAt = DateTimeOffset.UtcNow
                },
                new ClinicalUser
                {
                    Id = Guid.Parse("22222222-2222-2222-2222-222222222222"),
                    Username = "physician.demo",
                    PasswordHash = BCrypt.Net.BCrypt.HashPassword("DemoPhysician1!"),
                    DisplayName = "Dr. Demo Physician",
                    Role = ClinicalRole.Physician,
                    CreatedAt = DateTimeOffset.UtcNow
                },
                new ClinicalUser
                {
                    Id = Guid.Parse("33333333-3333-3333-3333-333333333333"),
                    Username = "admin.demo",
                    PasswordHash = BCrypt.Net.BCrypt.HashPassword("DemoAdmin1!"),
                    DisplayName = "Demo Admin",
                    Role = ClinicalRole.Admin,
                    CreatedAt = DateTimeOffset.UtcNow
                },
                new ClinicalUser
                {
                    Id = Guid.Parse("44444444-4444-4444-4444-444444444444"),
                    Username = "integration.mirth",
                    PasswordHash = BCrypt.Net.BCrypt.HashPassword("MirthIntegration1!"),
                    DisplayName = "Mirth Connect",
                    Role = ClinicalRole.Integration,
                    CreatedAt = DateTimeOffset.UtcNow
                });

            await db.SaveChangesAsync();
        }

        if (simulationEnabled)
            await EnsureSimulationRunnerAsync(db, simulationRunnerPassword);
    }

    /// <summary>
    /// Seeds the loopback simulation runner account when Simulation:Enabled.
    /// Idempotent — safe to call on an existing database that already has demo users.
    /// </summary>
    public static async Task EnsureSimulationRunnerAsync(
        AppDbContext db, string? password)
    {
        if (await db.ClinicalUsers.AnyAsync(u => u.Username == "simulation.runner"))
            return;

        if (string.IsNullOrWhiteSpace(password))
            throw new InvalidOperationException(
                "Simulation:Enabled requires Simulation:RunnerPassword to seed simulation.runner.");

        db.ClinicalUsers.Add(new ClinicalUser
        {
            Id = SimulationRunnerUserId,
            Username = "simulation.runner",
            PasswordHash = BCrypt.Net.BCrypt.HashPassword(password),
            DisplayName = "Simulation Runner",
            Role = ClinicalRole.Integration,
            CreatedAt = DateTimeOffset.UtcNow
        });

        await db.SaveChangesAsync();
    }
}
