namespace WalletService.Infrastructure.EntityConfigurations;

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

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

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

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

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

        builder.Property(h => h.OriginalAmount)
            .HasColumnName("original_amount")
            .HasPrecision(18, 2)
            .IsRequired();

        builder.Property(h => h.RemainingAmount)
            .HasColumnName("remaining_amount")
            .HasPrecision(18, 2)
            .IsRequired();

        builder.Property(h => h.ExecutedAmount)
            .HasColumnName("executed_amount")
            .HasPrecision(18, 2)
            .IsRequired();

        builder.Property(h => h.ReleasedAmount)
            .HasColumnName("released_amount")
            .HasPrecision(18, 2)
            .IsRequired();

        builder.Property(h => h.CurrencyTypeId)
            .HasColumnName("currency_type_id")
            .IsRequired();

        builder.HasOne(h => h.CurrencyType)
            .WithMany()
            .HasForeignKey(h => h.CurrencyTypeId)
            .OnDelete(DeleteBehavior.Restrict);

        builder.Property(h => h.ReferenceType)
            .HasColumnName("reference_type")
            .HasMaxLength(50)
            .IsRequired();

        builder.Property(h => h.ReferenceId)
            .HasColumnName("reference_id")
            .IsRequired();

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

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

        builder.HasOne(h => h.Status)
            .WithMany()
            .HasForeignKey(h => h.StatusId)
            .OnDelete(DeleteBehavior.Restrict);

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

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

        builder.Property(h => h.ExpiresAt)
            .HasColumnName("expires_at");

        // Indexes
        builder.HasIndex(h => h.WalletId)
            .HasDatabaseName("ix_wallet_holds_wallet_id");

        builder.HasIndex(h => new { h.ReferenceType, h.ReferenceId })
            .HasDatabaseName("ix_wallet_holds_reference");

        builder.HasIndex(h => h.StatusId)
            .HasDatabaseName("ix_wallet_holds_status_id");

        // Ignore computed properties
        builder.Ignore(h => h.IsExpired);
        builder.Ignore(h => h.IsActive);
    }
}
