namespace WalletService.Infrastructure.EntityConfigurations;

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

/// <summary>
/// EN: EF Core configuration for Payment aggregate.
/// VI: Cau hinh EF Core cho aggregate Payment.
/// </summary>
public class PaymentEntityTypeConfiguration : IEntityTypeConfiguration<Payment>
{
    public void Configure(EntityTypeBuilder<Payment> builder)
    {
        builder.ToTable("payments");

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

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

        builder.Property<Guid>("_orderId")
            .HasColumnName("order_id")
            .IsRequired();

        builder.Property<decimal>("_amount")
            .HasColumnName("amount")
            .HasPrecision(18, 2)
            .IsRequired();

        builder.Property<string>("_currency")
            .HasColumnName("currency")
            .HasMaxLength(10)
            .IsRequired();

        builder.Property<string>("_gatewayName")
            .HasColumnName("gateway_name")
            .HasMaxLength(50)
            .IsRequired();

        builder.Property<string?>("_transactionId")
            .HasColumnName("transaction_id")
            .HasMaxLength(255);

        builder.Property<string?>("_paymentUrl")
            .HasColumnName("payment_url")
            .HasMaxLength(2048);

        builder.Property<int>("_statusId")
            .HasColumnName("status_id")
            .IsRequired();

        builder.Property<string?>("_errorCode")
            .HasColumnName("error_code")
            .HasMaxLength(50);

        builder.Property<string?>("_errorMessage")
            .HasColumnName("error_message")
            .HasMaxLength(500);

        builder.Property<DateTime>("_createdAt")
            .HasColumnName("created_at")
            .IsRequired();

        builder.Property<DateTime?>("_completedAt")
            .HasColumnName("completed_at");

        // EN: Ignore computed properties and domain events (NOT expression-bodied properties backed by mapped fields).
        // VI: Bo qua cac property tinh toan va domain events (KHONG ignore cac property co backing field da map).
        builder.Ignore(p => p.Status);
        builder.Ignore(p => p.DomainEvents);

        // EN: Indexes for common queries
        // VI: Index cho cac truy van thuong dung
        builder.HasIndex("_orderId")
            .HasDatabaseName("ix_payments_order_id");

        builder.HasIndex("_transactionId")
            .HasDatabaseName("ix_payments_transaction_id");

        builder.HasIndex("_statusId")
            .HasDatabaseName("ix_payments_status_id");

        builder.HasIndex("_gatewayName")
            .HasDatabaseName("ix_payments_gateway_name");
    }
}
