using AdsServingService.Domain.AggregatesModel.FrequencyAggregate;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace AdsServingService.Infrastructure.EntityConfigurations;

/// <summary>
/// EN: EF Core entity configuration for FrequencyCap aggregate.
/// VI: Cấu hình entity EF Core cho aggregate FrequencyCap.
/// </summary>
public class FrequencyCapEntityTypeConfiguration : IEntityTypeConfiguration<FrequencyCap>
{
    public void Configure(EntityTypeBuilder<FrequencyCap> builder)
    {
        // Table mapping
        builder.ToTable("frequency_caps");

        // Primary key
        builder.HasKey(fc => fc.Id);
        builder.Property(fc => fc.Id)
            .HasColumnName("id")
            .ValueGeneratedNever();

        // Properties
        builder.Property<Guid>("_adId")
            .HasColumnName("ad_id")
            .IsRequired();

        builder.Property<int>("_maxImpressionsPerUser")
            .HasColumnName("max_impressions_per_user")
            .IsRequired();

        builder.Property<FrequencyWindow>("_window")
            .HasColumnName("window")
            .HasConversion<string>()
            .HasMaxLength(20)
            .IsRequired();

        // Indexes
        // EN: Index on ad_id for ad-specific frequency cap lookups
        // VI: Index trên ad_id để tra cứu frequency cap theo quảng cáo
        builder.HasIndex("_adId")
            .HasDatabaseName("ix_frequency_caps_ad_id");
    }
}
