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

namespace Infrastructure.Persistence.Configurations.Wallets;

public sealed class WalletTransactionConfiguration : IEntityTypeConfiguration<WalletTransaction>
{
    public void Configure(EntityTypeBuilder<WalletTransaction> builder)
    {
        builder.HasIndex(x => x.WalletKey);
        builder.HasIndex(x => x.CreatedAt);
        builder.HasIndex(x => new { x.WalletKey, x.CreatedAt });

        builder.ToTable(nameof(WalletTransaction), t => t.HasComment("회원 거래 장부"));
        builder.HasKey(x => x.ID);
        builder.Property(x => x.WalletKey).IsRequired();
        builder.Property(x => x.BalanceType).HasConversion<int>().IsRequired();
        builder.Property(x => x.TxType).HasConversion<int>().IsRequired();
        builder.Property(x => x.Reason).HasMaxLength(1000).IsRequired();
        builder.Property(x => x.RefID).HasMaxLength(100);
        builder.Property(x => x.UserID).HasMaxLength(100);
        builder.Property(x => x.Memo).HasMaxLength(500);
        builder.Property(x => x.CreatedAt).IsRequired();

        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.OwnsOne(x => x.BalanceAfter, money =>
        {
            money.Property(p => p.Value).HasPrecision(18, 0).HasColumnName("BalanceAfter").IsRequired();

            money.Property(p => p.Currency).HasColumnName("BalanceAfterCurrency").HasMaxLength(10).IsRequired();
        });
    }
}