using Lada.Framework.Data.Domains.Finance;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Lada.Framework.Data.Mappings.EntityMappings.Finance
{
    public class TransferPaymentBillOrderMap : IEntityMappingConfiguration<TransferPaymentBillOrder>
    {
        public void Map(EntityTypeBuilder<TransferPaymentBillOrder> builder)
        {
            builder.ToTable("TransferPaymentBillOrders");

            // Primary Key
            builder.HasKey(x => x.Id);

            // Properties
            builder.Property(x => x.OrderCode)
                .HasMaxLength(128)
                .IsRequired();

            builder.Property(x => x.Note)
                .HasMaxLength(512);

            builder.Property(x => x.CreatedByUserName)
                .HasMaxLength(128)
                .IsRequired();

            builder.Property(x => x.UpdatedByUserName)
                .HasMaxLength(128)
                .IsRequired();

            // Decimal precision
            builder.Property(x => x.TransferFeeAmount)
                .HasPrecision(18, 2);

            // Relationships
            builder.HasOne(x => x.TransferPaymentBill)
                .WithMany(x => x.Orders)
                .HasForeignKey(x => x.TransferPaymentBillId)
                .OnDelete(DeleteBehavior.Cascade);

            builder.HasOne(x => x.Order)
                .WithMany()
                .HasForeignKey(x => x.OrderId)
                .OnDelete(DeleteBehavior.Restrict);

            // Indexes
            builder.HasIndex(x => x.TransferPaymentBillId);

            builder.HasIndex(x => x.OrderId);

            builder.HasIndex(x => x.OrderCode);

            // Đảm bảo một đơn hàng chỉ thuộc về một hóa đơn
            builder.HasIndex(x => new { x.TransferPaymentBillId, x.OrderId })
                .IsUnique();
        }
    }
}
