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

namespace AdsTrackingService.Infrastructure.EntityConfigurations;

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

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

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

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

        builder.Property(c => c.UserId)
            .HasColumnName("user_id")
            .IsRequired();

        builder.Property(c => c.ConversionType)
            .HasColumnName("conversion_type")
            .HasMaxLength(50)
            .IsRequired();

        builder.Property(c => c.ConversionValue)
            .HasColumnName("conversion_value")
            .HasPrecision(18, 2)
            .IsRequired();

        builder.Property(c => c.Currency)
            .HasColumnName("currency")
            .HasMaxLength(3) // EN: ISO 4217 currency code / VI: Mã tiền tệ ISO 4217
            .IsRequired();

        builder.Property(c => c.ConversionTime)
            .HasColumnName("conversion_time")
            .IsRequired();

        // EN: Indexes / VI: Các index
        builder.HasIndex(c => c.AdvertiserId)
            .HasDatabaseName("ix_conversions_advertiser_id");

        builder.HasIndex(c => new { c.CampaignId, c.ConversionTime })
            .HasDatabaseName("ix_conversions_campaign_id_conversion_time");

        builder.HasIndex(c => new { c.UserId, c.ConversionTime })
            .HasDatabaseName("ix_conversions_user_id_conversion_time");

        builder.HasIndex(c => c.ConversionType)
            .HasDatabaseName("ix_conversions_conversion_type");

        builder.HasIndex(c => c.ConversionTime)
            .HasDatabaseName("ix_conversions_conversion_time");
    }
}
