using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using AdsBillingService.Domain.AggregatesModel.BillingAccountAggregate;

namespace AdsBillingService.Infrastructure.EntityConfigurations;

/// <summary>
/// EN: Entity configuration for BillingAccount aggregate.
/// VI: Cấu hình entity cho aggregate BillingAccount.
/// </summary>
public class BillingAccountEntityTypeConfiguration : IEntityTypeConfiguration<BillingAccount>
{
    public void Configure(EntityTypeBuilder<BillingAccount> builder)
    {
        // EN: Table name / VI: Tên bảng
        builder.ToTable("billing_accounts");

        // EN: Primary key / VI: Khóa chính
        builder.HasKey(b => b.Id);
        builder.Property(b => b.Id)
            .HasColumnName("id")
            .IsRequired();

        // EN: Advertiser ID / VI: ID Advertiser
        builder.Property(b => b.AdvertiserId)
            .HasColumnName("advertiser_id")
            .IsRequired();

        builder.HasIndex(b => b.AdvertiserId)
            .HasDatabaseName("ix_billing_accounts_advertiser_id");

        // EN: Wallet ID / VI: ID Ví
        builder.Property(b => b.WalletId)
            .HasColumnName("wallet_id")
            .IsRequired(false);

        // EN: Payment method / VI: Phương thức thanh toán
        builder.Property(b => b.PaymentMethod)
            .HasColumnName("payment_method")
            .HasConversion<int>()
            .IsRequired();

        // EN: Account status / VI: Trạng thái tài khoản
        builder.Property(b => b.Status)
            .HasColumnName("status")
            .HasConversion<int>()
            .IsRequired();

        // EN: Balance / VI: Số dư
        builder.Property(b => b.Balance)
            .HasColumnName("balance")
            .HasColumnType("decimal(18,2)")
            .IsRequired();

        // EN: Credit limit / VI: Hạn mức tín dụng
        builder.Property(b => b.CreditLimit)
            .HasColumnName("credit_limit")
            .HasColumnType("decimal(18,2)")
            .IsRequired();

        // EN: Billing threshold as owned entity (value object) / VI: Ngưỡng billing như owned entity
        builder.OwnsOne(b => b.Threshold, threshold =>
        {
            threshold.Property(t => t.Amount)
                .HasColumnName("threshold_amount")
                .HasColumnType("decimal(18,2)");

            threshold.Property(t => t.AutoCharge)
                .HasColumnName("threshold_auto_charge");
        });

        // EN: Navigation for owned entity / VI: Navigation cho owned entity
        builder.Navigation(b => b.Threshold).IsRequired(false);

        // EN: Timestamps / VI: Thời gian
        builder.Property(b => b.CreatedAt)
            .HasColumnName("created_at")
            .IsRequired();

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

        // EN: Indexes / VI: Chỉ mục
        builder.HasIndex(b => b.CreatedAt)
            .HasDatabaseName("ix_billing_accounts_created_at");
    }
}
