using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using AdsTrackingService.Domain.AggregatesModel.AttributionAggregate;

namespace AdsTrackingService.Infrastructure.EntityConfigurations;

/// <summary>
/// EN: Entity configuration for Attribution aggregate root.
/// VI: Cấu hình entity cho Attribution aggregate root.
/// </summary>
public class AttributionEntityTypeConfiguration : IEntityTypeConfiguration<Attribution>
{
    public void Configure(EntityTypeBuilder<Attribution> builder)
    {
        // EN: Table mapping / VI: Ánh xạ bảng
        builder.ToTable("attributions", "ads_tracking");

        // EN: Primary key / VI: Khóa chính
        builder.HasKey(a => a.Id);
        builder.Property(a => a.Id)
            .HasColumnName("id")
            .IsRequired();

        // EN: Properties / VI: Các thuộc tính
        builder.Property(a => a.ConversionId)
            .HasColumnName("conversion_id")
            .IsRequired();

        builder.Property(a => a.AdId)
            .HasColumnName("ad_id")
            .IsRequired();

        builder.Property(a => a.CampaignId)
            .HasColumnName("campaign_id")
            .IsRequired();

        builder.Property(a => a.Model)
            .HasColumnName("model")
            .IsRequired();

        builder.Property(a => a.AttributedValue)
            .HasColumnName("attributed_value")
            .HasPrecision(18, 2)
            .IsRequired();

        builder.Property(a => a.AttributedAt)
            .HasColumnName("attributed_at")
            .IsRequired();

        // EN: Indexes / VI: Các index
        builder.HasIndex(a => a.ConversionId)
            .HasDatabaseName("ix_attributions_conversion_id");

        builder.HasIndex(a => new { a.AdId, a.AttributedAt })
            .HasDatabaseName("ix_attributions_ad_id_attributed_at");

        builder.HasIndex(a => new { a.CampaignId, a.AttributedAt })
            .HasDatabaseName("ix_attributions_campaign_id_attributed_at");

        builder.HasIndex(a => a.Model)
            .HasDatabaseName("ix_attributions_model");

        // EN: Composite index for analytics / VI: Index tổng hợp cho phân tích
        builder.HasIndex(a => new { a.Model, a.CampaignId, a.AttributedAt })
            .HasDatabaseName("ix_attributions_model_campaign_id_attributed_at");
    }
}
