using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using AdsAnalyticsService.Domain.AggregatesModel.ReportAggregate;

namespace AdsAnalyticsService.Infrastructure.EntityConfigurations;

/// <summary>
/// EN: Entity type configuration for Report aggregate.
/// VI: Cấu hình entity type cho Report aggregate.
/// </summary>
public class ReportEntityTypeConfiguration : IEntityTypeConfiguration<Report>
{
    public void Configure(EntityTypeBuilder<Report> builder)
    {
        // EN: Table name
        // VI: Tên bảng
        builder.ToTable("reports");

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

        // EN: Advertiser reference
        // VI: Tham chiếu advertiser
        builder.Property(r => r.AdvertiserId)
            .HasColumnName("advertiser_id")
            .IsRequired();

        builder.HasIndex(r => r.AdvertiserId)
            .HasDatabaseName("idx_reports_advertiser_id");

        // EN: Report properties
        // VI: Thuộc tính báo cáo
        builder.Property(r => r.Name)
            .HasColumnName("name")
            .HasMaxLength(200)
            .IsRequired();

        builder.Property(r => r.ReportType)
            .HasColumnName("report_type")
            .HasConversion<int>()
            .IsRequired();

        builder.Property(r => r.StartDate)
            .HasColumnName("start_date")
            .HasColumnType("date")
            .IsRequired();

        builder.Property(r => r.EndDate)
            .HasColumnName("end_date")
            .HasColumnType("date")
            .IsRequired();

        builder.Property(r => r.Status)
            .HasColumnName("status")
            .HasConversion<int>()
            .IsRequired();

        builder.Property(r => r.DataJson)
            .HasColumnName("data_json")
            .HasColumnType("jsonb")
            .IsRequired(false);

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

        // EN: Indexes
        // VI: Chỉ mục
        builder.HasIndex(r => r.Status)
            .HasDatabaseName("idx_reports_status");

        builder.HasIndex(r => r.CreatedAt)
            .HasDatabaseName("idx_reports_created_at");

        // EN: Ignore domain events
        // VI: Bỏ qua domain events
        builder.Ignore(r => r.DomainEvents);
    }
}
