using HotelPosApi.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace HotelPosApi.Data;

public static class DbInitializer
{
    public static async Task SeedAdminAsync(AppDbContext db, IConfiguration config, ILogger logger)
    {
        var username = config["SeedAdmin:Username"] ?? "admin";
        var password = config["SeedAdmin:Password"] ?? "Admin@123";
        var role = config["SeedAdmin:Role"] ?? "Admin";

        if (await db.Users.AnyAsync(u => u.Username == username))
            return;

        var hasher = new PasswordHasher<string>();
        var hash = hasher.HashPassword(username, password);

        db.Users.Add(new User
        {
            Username = username,
            PasswordHash = hash,
            Role = role,
            CreatedAtUtc = DateTime.UtcNow,
        });

        await db.SaveChangesAsync();
        logger.LogInformation("Seeded default admin user: {Username}", username);
    }
}
