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

namespace MembershipService.Infrastructure.EntityConfigurations;

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

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

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

        // EN: Level number (unique)
        // VI: Số thứ tự level (unique)
        builder.Property("_levelNumber")
            .HasColumnName("level_number")
            .IsRequired();

        builder.HasIndex("_levelNumber")
            .IsUnique()
            .HasDatabaseName("ix_level_definitions_level_number");

        // EN: Level name
        // VI: Tên level
        builder.Property("_name")
            .HasColumnName("name")
            .HasMaxLength(100)
            .IsRequired();

        // EN: Required EXP
        // VI: EXP yêu cầu
        builder.Property("_requiredExp")
            .HasColumnName("required_exp")
            .IsRequired()
            .HasDefaultValue(0);

        // EN: Description
        // VI: Mô tả
        builder.Property("_description")
            .HasColumnName("description");

        // EN: Icon URL
        // VI: URL icon
        builder.Property("_iconUrl")
            .HasColumnName("icon_url")
            .HasMaxLength(500);

        // EN: Badge color
        // VI: Màu badge
        builder.Property("_badgeColor")
            .HasColumnName("badge_color")
            .HasMaxLength(20);

        // EN: Active status
        // VI: Trạng thái active
        builder.Property("_isActive")
            .HasColumnName("is_active")
            .IsRequired()
            .HasDefaultValue(true);

        builder.HasIndex("_isActive")
            .HasDatabaseName("ix_level_definitions_is_active");

        // EN: Timestamps
        // VI: Timestamps
        builder.Property("_createdAt")
            .HasColumnName("created_at")
            .IsRequired();

        builder.Property("_updatedAt")
            .HasColumnName("updated_at")
            .IsRequired();

        // EN: Relationship with benefits
        // VI: Quan hệ với benefits
        builder.HasMany(l => l.Benefits)
            .WithOne()
            .HasForeignKey("_levelDefinitionId")
            .OnDelete(DeleteBehavior.Cascade);

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