using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace CMSMicroservice.Infrastructure.Persistence.Configurations;

/// <summary>
/// تنظیمات پویای سیستم
/// </summary>
public class SystemConfigurationConfiguration : IEntityTypeConfiguration<SystemConfiguration>
{
    public void Configure(EntityTypeBuilder<SystemConfiguration> builder)
    {
        builder.HasQueryFilter(p => !p.IsDeleted);
        builder.Ignore(entity => entity.DomainEvents);
        
        builder.HasKey(entity => entity.Id);
        builder.Property(entity => entity.Id).UseIdentityColumn();
        
        builder.Property(entity => entity.Scope).IsRequired();
        builder.Property(entity => entity.Key).IsRequired().HasMaxLength(200);
        builder.Property(entity => entity.Value).IsRequired().HasMaxLength(1000);
        builder.Property(entity => entity.DataType).IsRequired(false).HasMaxLength(50);
        builder.Property(entity => entity.Description).IsRequired(false).HasMaxLength(500);
        builder.Property(entity => entity.IsActive).IsRequired();
        
        // Composite Index برای جستجوی سریع
        builder.HasIndex(e => new { e.Scope, e.Key })
            .IsUnique()
            .HasDatabaseName("IX_SystemConfiguration_Scope_Key");
        
        // Index برای IsActive
        builder.HasIndex(e => e.IsActive)
            .HasDatabaseName("IX_SystemConfiguration_IsActive");
    }
}
