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

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

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

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

            builder.Property(x => x.PartnerCode)
                .HasMaxLength(256);

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

            builder.Property(x => x.ReceiverName)
                .HasMaxLength(256);

            builder.Property(x => x.ReceiverPhone)
                .HasMaxLength(32);

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

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

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

            builder.Property(x => x.AppliedFeeAmount)
                .HasPrecision(18, 2);

            builder.Property(x => x.NetAmount)
                .HasPrecision(18, 2);

            builder.Property(x => x.ChargeWeight)
                .HasPrecision(18, 2);

            builder.Property(x => x.ShippingFee)
                .HasPrecision(18, 2);

            // Relationships
            builder.HasOne(x => x.CustomerReconciliationSlip)
                .WithMany(x => x.Details)
                .HasForeignKey(x => x.CustomerReconciliationSlipId)
                .OnDelete(DeleteBehavior.Cascade);

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

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

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

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