// EN: Database seeder for initial data.
// VI: Database seeder cho dữ liệu khởi tạo.

using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using IamService.Domain.AggregatesModel.RoleAggregate;

namespace IamService.Infrastructure.Data;

/// <summary>
/// EN: Database seeder for system roles and initial data.
/// VI: Database seeder cho system roles và dữ liệu khởi tạo.
/// </summary>
public static class DataSeeder
{
    /// <summary>
    /// EN: Seed system roles into the database.
    /// VI: Seed system roles vào database.
    /// </summary>
    public static async Task SeedRolesAsync(IServiceProvider serviceProvider)
    {
        using var scope = serviceProvider.CreateScope();
        var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<ApplicationRole>>();
        var logger = scope.ServiceProvider.GetRequiredService<ILogger<RoleManager<ApplicationRole>>>();

        // EN: Define system roles
        // VI: Định nghĩa system roles
        var systemRoles = new (string Name, string Description)[]
        {
            // Customer roles
            ("User", "Regular user with basic access"),
            ("PremiumUser", "Premium subscriber with enhanced access"),
            
            // Merchant roles
            ("Merchant", "Shop owner with full merchant access"),
            ("MerchantStaff", "Shop staff with limited access"),
            ("MerchantAdmin", "Merchant administrator with management access"),
            
            // Platform roles
            ("Admin", "Platform administrator"),
            ("SuperAdmin", "Super administrator with full system access"),
            ("Support", "Customer support agent"),
        };

        foreach (var (name, description) in systemRoles)
        {
            if (!await roleManager.RoleExistsAsync(name))
            {
                var role = new ApplicationRole(name, description, isSystemRole: true);
                var result = await roleManager.CreateAsync(role);

                if (result.Succeeded)
                {
                    logger.LogInformation(
                        "EN: Created system role '{RoleName}' / VI: Đã tạo system role '{RoleName}'",
                        name);
                }
                else
                {
                    var errors = string.Join(", ", result.Errors.Select(e => e.Description));
                    logger.LogWarning(
                        "EN: Failed to create role '{RoleName}': {Errors} / VI: Không thể tạo role '{RoleName}': {Errors}",
                        name, errors);
                }
            }
            else
            {
                logger.LogDebug(
                    "EN: Role '{RoleName}' already exists / VI: Role '{RoleName}' đã tồn tại",
                    name);
            }
        }

        logger.LogInformation(
            "EN: System roles seeding completed / VI: Seeding system roles hoàn thành");
    }
}
