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

namespace AdsServingService.Infrastructure.EntityConfigurations;

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

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

        // Properties
        builder.Property<Guid>("_campaignId")
            .HasColumnName("campaign_id")
            .IsRequired();

        builder.Property<decimal>("_dailyBudget")
            .HasColumnName("daily_budget")
            .HasColumnType("decimal(18,4)")
            .IsRequired();

        builder.Property<decimal>("_spentToday")
            .HasColumnName("spent_today")
            .HasColumnType("decimal(18,4)")
            .IsRequired();

        builder.Property<PacingStrategy>("_strategy")
            .HasColumnName("strategy")
            .HasConversion<string>()
            .HasMaxLength(20)
            .IsRequired();

        builder.Property<DateTime>("_lastUpdated")
            .HasColumnName("last_updated")
            .IsRequired();

        // Indexes
        // EN: Unique index on campaign_id (one pacer per campaign)
        // VI: Index duy nhất trên campaign_id (một pacer cho mỗi campaign)
        builder.HasIndex("_campaignId")
            .IsUnique()
            .HasDatabaseName("ix_budget_pacers_campaign_id");

        // EN: Index on last_updated for finding stale pacers
        // VI: Index trên last_updated để tìm các pacer cũ
        builder.HasIndex("_lastUpdated")
            .HasDatabaseName("ix_budget_pacers_last_updated");
    }
}
