using Domain.Entities.Wallets;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Infrastructure.Persistence.Configurations.Wallets;

public sealed class WalletBalanceConfiguration : IEntityTypeConfiguration<WalletBalance>
{
    public void Configure(EntityTypeBuilder<WalletBalance> builder)
    {
        builder.ToTable(nameof(WalletBalance), t => t.HasComment("회원 지갑 잔액"));

        builder.HasKey(x => x.ID);
        builder.Property(x => x.WalletKey).IsRequired();
        builder.Property(x => x.Type).HasConversion<int>().IsRequired();

        // Money ValueObject
        builder.OwnsOne(x => x.Amount, money =>
        {
            money.Property(p => p.Value).HasPrecision(18, 0).HasColumnName("Amount").IsRequired();
            money.Property(p => p.Currency).HasColumnName("Currency").HasMaxLength(10).IsRequired();
        });

        builder.HasIndex(x => new { x.WalletKey, x.Type }).IsUnique();
    }
}