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

namespace Infrastructure.Persistence.Configurations.Wallets;

public sealed class WalletConfiguration : IEntityTypeConfiguration<Wallet>
{
    public void Configure(EntityTypeBuilder<Wallet> builder)
    {
        builder.HasMany(x => x.Balances).WithOne().HasForeignKey(x => x.WalletKey).HasPrincipalKey(x => x.WalletKey).OnDelete(DeleteBehavior.Cascade);
        builder.Navigation(x => x.Balances).HasField("_balances").UsePropertyAccessMode(PropertyAccessMode.Field);
        builder.HasMany(x => x.Transactions).WithOne(x => x.Wallet).HasForeignKey(x => x.WalletKey).HasPrincipalKey(x => x.WalletKey).OnDelete(DeleteBehavior.Cascade);
        builder.Navigation(x => x.Transactions).HasField("_transactions").UsePropertyAccessMode(PropertyAccessMode.Field);
        builder.HasIndex(x => x.WalletKey).IsUnique();

        builder.ToTable(nameof(Wallet), t => t.HasComment("회원 지갑"));
        builder.HasKey(x => x.ID);
        builder.Property(x => x.WalletKey).IsRequired();
        builder.Property(x => x.MemberID).IsRequired();
        builder.Property(x => x.CreatedAt).IsRequired();
        builder.Property(x => x.UpdatedAt);
    }
}