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

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

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

            // Properties
            builder.Property(x => x.Code)
                .HasMaxLength(32)
                .IsRequired();

            

            builder.Property(x => x.Note)
                .HasDefaultValue(string.Empty)
                .HasMaxLength(1024);

            builder.Property(x => x.TransferInfo)
                .HasMaxLength(1024);

            builder.Property(x => x.ApprovedByUsername)
                .HasMaxLength(128);

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

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

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

            // Relationships
            builder.HasOne(x => x.Customer)
                .WithMany()
                .HasForeignKey(x => x.CustomerId)
                .OnDelete(DeleteBehavior.Restrict);

            builder.HasMany(x => x.Orders)
                .WithOne(x => x.TransferPaymentBill)
                .HasForeignKey(x => x.TransferPaymentBillId)
                .OnDelete(DeleteBehavior.Cascade);

            builder.HasOne(x => x.Media)
                .WithMany()
                .HasForeignKey(x => x.MediaId)
                .OnDelete(DeleteBehavior.SetNull);

            builder.HasOne(x => x.Transaction)
                .WithMany()
                .HasForeignKey(x => x.TransactionId)
                .OnDelete(DeleteBehavior.SetNull);

            // Indexes
            builder.HasIndex(x => x.Code)
                .IsUnique(); // QUAN TRỌNG: Đảm bảo mã hóa đơn không trùng

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

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

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

            

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

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

            builder.HasIndex(x => x.MediaId);
        }
    }
}
