namespace WalletService.Infrastructure.EntityConfigurations;

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WalletService.Domain.AggregatesModel.WalletAggregate;

/// <summary>
/// EN: EF Core configuration for WalletItem entity.
/// VI: Cấu hình EF Core cho entity WalletItem.
/// </summary>
public class WalletItemEntityTypeConfiguration : IEntityTypeConfiguration<WalletItem>
{
    public void Configure(EntityTypeBuilder<WalletItem> builder)
    {
        builder.ToTable("wallet_items");

        builder.HasKey(wi => wi.Id);

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

        builder.Property(wi => wi.WalletId)
            .HasColumnName("wallet_id")
            .IsRequired();

        builder.Property(wi => wi.CurrencyTypeId)
            .HasColumnName("currency_type_id")
            .IsRequired();

        // EN: Ignore navigation property - CurrencyType is an enumeration
        // VI: Bỏ qua navigation property - CurrencyType là enumeration
        builder.Ignore(wi => wi.CurrencyType);

        builder.Property(wi => wi.Balance)
            .HasColumnName("balance")
            .HasColumnType("decimal(18,2)")
            .IsRequired();

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

        builder.Property(wi => wi.UpdatedAt)
            .HasColumnName("updated_at")
            .IsRequired();

        // EN: Unique constraint: one balance per currency per wallet
        // VI: Ràng buộc unique: một số dư cho mỗi loại tiền tệ trong mỗi ví
        builder.HasIndex(wi => new { wi.WalletId, wi.CurrencyTypeId })
            .IsUnique()
            .HasDatabaseName("ix_wallet_items_wallet_currency");

        builder.HasIndex(wi => wi.WalletId)
            .HasDatabaseName("ix_wallet_items_wallet_id");

        builder.Ignore(wi => wi.DomainEvents);
    }
}
