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

namespace AdsTrackingService.Infrastructure.EntityConfigurations;

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

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

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

        builder.Property(p => p.PixelCode)
            .HasColumnName("pixel_code")
            .HasMaxLength(16)
            .IsRequired();

        builder.Property(p => p.IsActive)
            .HasColumnName("is_active")
            .IsRequired();

        builder.Property(p => p.CreatedAt)
            .HasColumnName("created_at")
            .IsRequired();

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

        builder.HasIndex(p => p.PixelCode)
            .IsUnique()
            .HasDatabaseName("uix_tracking_pixels_pixel_code");

        builder.HasIndex(p => p.IsActive)
            .HasDatabaseName("ix_tracking_pixels_is_active");
    }
}
