namespace WalletService.Infrastructure.EntityConfigurations;

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

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

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

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

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

        builder.HasIndex(t => t.WalletId)
            .HasDatabaseName("ix_wallet_transactions_wallet_id");

        // EN: Configure Money value object for Amount
        // VI: Cấu hình value object Money cho Amount
        builder.OwnsOne(t => t.Amount, amount =>
        {
            amount.Property(m => m.Amount)
                .HasColumnName("amount")
                .HasColumnType("decimal(18,2)")
                .IsRequired();

            amount.Property(m => m.Currency)
                .HasColumnName("currency")
                .HasMaxLength(3)
                .IsRequired();
        });

        builder.Property(t => t.TypeId)
            .HasColumnName("type_id")
            .IsRequired();

        builder.Ignore(t => t.Type);

        builder.Property(t => t.ReferenceId)
            .HasColumnName("reference_id")
            .HasMaxLength(100);

        builder.Property(t => t.Description)
            .HasColumnName("description")
            .HasMaxLength(500)
            .IsRequired();

        builder.Property(t => t.BalanceAfter)
            .HasColumnName("balance_after")
            .HasColumnType("decimal(18,2)")
            .IsRequired();

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

        builder.HasIndex(t => t.CreatedAt)
            .HasDatabaseName("ix_wallet_transactions_created_at");

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