using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using MembershipService.Domain.AggregatesModel.ExperienceAggregate;

namespace MembershipService.Infrastructure.EntityConfigurations;

/// <summary>
/// EN: Entity configuration for ExperienceTransaction entity.
/// VI: Cấu hình entity cho ExperienceTransaction entity.
/// </summary>
public class ExperienceTransactionEntityTypeConfiguration : IEntityTypeConfiguration<ExperienceTransaction>
{
    public void Configure(EntityTypeBuilder<ExperienceTransaction> builder)
    {
        builder.ToTable("experience_transactions");

        // EN: Primary key
        // VI: Primary key
        builder.HasKey(t => t.Id);

        builder.Property(t => t.Id)
            .HasColumnName("id")
            .ValueGeneratedNever();

        // EN: Member ID (foreign key)
        // VI: ID member (foreign key)
        builder.Property("_memberId")
            .HasColumnName("member_id")
            .IsRequired();

        builder.HasIndex("_memberId")
            .HasDatabaseName("ix_experience_transactions_member_id");

        // EN: Points
        // VI: Điểm
        builder.Property("_points")
            .HasColumnName("points")
            .IsRequired();

        // EN: Source ID
        // VI: ID nguồn
        builder.Property("_sourceId")
            .HasColumnName("source_id")
            .IsRequired();

        builder.HasIndex("_sourceId")
            .HasDatabaseName("ix_experience_transactions_source");

        // EN: Reference ID (Order ID, etc.)
        // VI: ID tham chiếu (Order ID, etc.)
        builder.Property("_referenceId")
            .HasColumnName("reference_id")
            .HasMaxLength(100);

        // EN: Metadata (JSON)
        // VI: Metadata (JSON)
        builder.Property("_metadata")
            .HasColumnName("metadata")
            .HasColumnType("jsonb");

        // EN: Level at time of transaction
        // VI: Level tại thời điểm giao dịch
        builder.Property("_levelAtTime")
            .HasColumnName("level_at_time")
            .IsRequired();

        // EN: Created timestamp
        // VI: Thời gian tạo
        builder.Property("_createdAt")
            .HasColumnName("created_at")
            .IsRequired();

        builder.HasIndex("_createdAt")
            .HasDatabaseName("ix_experience_transactions_created_at");

        // EN: Ignore Source navigation property (it's an Enumeration)
        // VI: Bỏ qua navigation property Source (nó là Enumeration)
        builder.Ignore(t => t.Source);

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