using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using CMSMicroservice.Domain.Entities.Configuration; using CMSMicroservice.Domain.Enums; using System.Collections.Generic; namespace CMSMicroservice.Infrastructure.Persistence; public class ApplicationDbContextInitialiser { private readonly ApplicationDbContext _context; private readonly ILogger _logger; public ApplicationDbContextInitialiser(ApplicationDbContext context, ILogger logger) { _context = context; _logger = logger; } public async Task InitialiseAsync() { try { if (_context.Database.IsSqlServer()) { await _context.Database.MigrateAsync(); } } catch (Exception ex) { _logger.LogError(ex, "An error occurred while initialising the database."); throw; } } public async Task SeedAsync() { try { await TrySeedAsync(); } catch (Exception ex) { _logger.LogError(ex, "An error occurred while seeding the database."); throw; } } public async Task TrySeedAsync() { // Seed default System Configurations for Network-Club-Commission System if (!_context.SystemConfigurations.Any()) { var defaultConfigurations = new List { // Network Configuration new SystemConfiguration { Key = "Network.MaxDepth", Value = "10", Description = "حداکثر عمق شبکه باینری", Scope = ConfigurationScope.Network, IsActive = true }, new SystemConfiguration { Key = "Network.AllowOrphanNodes", Value = "false", Description = "اجازه حذف والدین که فرزند دارند", Scope = ConfigurationScope.Network, IsActive = true }, // Club Configuration new SystemConfiguration { Key = "Club.DefaultMembershipDurationMonths", Value = "12", Description = "مدت زمان پیش‌فرض عضویت باشگاه (ماه)", Scope = ConfigurationScope.Club, IsActive = true }, new SystemConfiguration { Key = "Club.MinimumActivationAmount", Value = "1000000", Description = "حداقل مبلغ برای فعال‌سازی عضویت (ریال)", Scope = ConfigurationScope.Club, IsActive = true }, // Commission Configuration new SystemConfiguration { Key = "Commission.WeeklyPoolContributionPercent", Value = "10", Description = "درصد مشارکت در استخر هفتگی از تعادل کل", Scope = ConfigurationScope.Commission, IsActive = true }, new SystemConfiguration { Key = "Commission.MinimumPayoutAmount", Value = "100000", Description = "حداقل مبلغ برای پرداخت کمیسیون (ریال)", Scope = ConfigurationScope.Commission, IsActive = true }, new SystemConfiguration { Key = "Commission.CashWithdrawalEnabled", Value = "true", Description = "امکان برداشت نقدی فعال باشد", Scope = ConfigurationScope.Commission, IsActive = true }, new SystemConfiguration { Key = "Commission.DiamondWithdrawalEnabled", Value = "true", Description = "امکان تبدیل به الماس فعال باشد", Scope = ConfigurationScope.Commission, IsActive = true }, // System Configuration new SystemConfiguration { Key = "System.MaintenanceMode", Value = "false", Description = "حالت تعمیر و نگهداری سیستم", Scope = ConfigurationScope.System, IsActive = true }, new SystemConfiguration { Key = "System.EnableAuditLog", Value = "true", Description = "فعال‌سازی لاگ تغییرات", Scope = ConfigurationScope.System, IsActive = true } }; await _context.SystemConfigurations.AddRangeAsync(defaultConfigurations); await _context.SaveChangesAsync(); _logger.LogInformation("Seeded {Count} default system configurations", defaultConfigurations.Count); } } }