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

namespace CMSMicroservice.Infrastructure.Persistence.Configurations;

/// <summary>
/// تنظیمات جدول تعریف هفته‌ها
/// </summary>
public class WeekDefinitionConfiguration : IEntityTypeConfiguration<WeekDefinition>
{
    public void Configure(EntityTypeBuilder<WeekDefinition> 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.WeekOrder).IsRequired();
        builder.Property(entity => entity.DisplayName).IsRequired().HasMaxLength(50);
        builder.Property(entity => entity.GregorianWeekNumber).IsRequired().HasMaxLength(20);
        builder.Property(entity => entity.PersianWeekNumber).IsRequired().HasMaxLength(20);
        builder.Property(entity => entity.StartDate).IsRequired();
        builder.Property(entity => entity.EndDate).IsRequired();
        builder.Property(entity => entity.GregorianYear).IsRequired();
        builder.Property(entity => entity.PersianYear).IsRequired();
        builder.Property(entity => entity.IsActive).IsRequired().HasDefaultValue(true);
        
        // Unique Index برای GregorianWeekNumber
        builder.HasIndex(e => e.GregorianWeekNumber)
            .IsUnique()
            .HasDatabaseName("IX_WeekDefinition_GregorianWeekNumber");
        
        // Index برای PersianWeekNumber
        builder.HasIndex(e => e.PersianWeekNumber)
            .HasDatabaseName("IX_WeekDefinition_PersianWeekNumber");
        
        // Index برای StartDate
        builder.HasIndex(e => e.StartDate)
            .HasDatabaseName("IX_WeekDefinition_StartDate");
        
        // Index برای سال‌ها
        builder.HasIndex(e => e.GregorianYear)
            .HasDatabaseName("IX_WeekDefinition_GregorianYear");
        
        builder.HasIndex(e => e.PersianYear)
            .HasDatabaseName("IX_WeekDefinition_PersianYear");
        
        builder.ToTable("WeekDefinitions");
    }
}
