namespace WalletService.Infrastructure.EntityConfigurations;

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

/// <summary>
/// EN: EF Core configuration for Wallet aggregate with multi-currency support.
/// VI: Cấu hình EF Core cho aggregate Wallet với hỗ trợ đa tiền tệ.
/// </summary>
public class WalletEntityTypeConfiguration : IEntityTypeConfiguration<Wallet>
{
    public void Configure(EntityTypeBuilder<Wallet> builder)
    {
        builder.ToTable("wallets");

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

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

        builder.Property(w => w.UserId)
            .HasColumnName("user_id")
            .IsRequired();

        builder.HasIndex(w => w.UserId)
            .IsUnique()
            .HasDatabaseName("ix_wallets_user_id");

        // EN: Default currency type ID
        // VI: ID loại tiền tệ mặc định
        builder.Property(w => w.DefaultCurrencyTypeId)
            .HasColumnName("default_currency_type_id")
            .IsRequired()
            .HasDefaultValue(1); // VND

        // EN: Ignore CurrencyType navigation - it's an enumeration
        // VI: Bỏ qua navigation CurrencyType - đây là enumeration
        builder.Ignore(w => w.DefaultCurrencyType);

        // EN: Ignore legacy Balance property (computed from Balances)
        // VI: Bỏ qua property Balance kế thừa (tính từ Balances)
        #pragma warning disable CS0618 // Obsolete warning suppressed for EF Core configuration
        builder.Ignore(w => w.Balance);
        #pragma warning restore CS0618

        builder.Property(w => w.StatusId)
            .HasColumnName("status_id")
            .IsRequired();

        builder.Ignore(w => w.Status);

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

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

        // EN: Configure navigation to wallet items (multi-balance)
        // VI: Cấu hình navigation đến wallet items (đa số dư)
        var balancesNavigation = builder.Metadata.FindNavigation(nameof(Wallet.Balances));
        balancesNavigation?.SetPropertyAccessMode(PropertyAccessMode.Field);

        builder.HasMany(w => w.Balances)
            .WithOne()
            .HasForeignKey(wi => wi.WalletId)
            .OnDelete(DeleteBehavior.Cascade);

        // EN: Configure navigation to transactions
        // VI: Cấu hình navigation đến transactions
        var transactionsNavigation = builder.Metadata.FindNavigation(nameof(Wallet.Transactions));
        transactionsNavigation?.SetPropertyAccessMode(PropertyAccessMode.Field);

        builder.HasMany(w => w.Transactions)
            .WithOne()
            .HasForeignKey(t => t.WalletId)
            .OnDelete(DeleteBehavior.Cascade);

        // EN: Configure navigation to holds (escrow)
        // VI: Cấu hình navigation đến holds (ký quỹ)
        var holdsNavigation = builder.Metadata.FindNavigation(nameof(Wallet.Holds));
        holdsNavigation?.SetPropertyAccessMode(PropertyAccessMode.Field);

        builder.HasMany(w => w.Holds)
            .WithOne()
            .HasForeignKey(h => h.WalletId)
            .OnDelete(DeleteBehavior.Cascade);

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