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 CustomerReconciliationSlipMap : IEntityMappingConfiguration<CustomerReconciliationSlip>
    {
        public void Map(EntityTypeBuilder<CustomerReconciliationSlip> builder)
        {
            builder.ToTable("CustomerReconciliationSlips");

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

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

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

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

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

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

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

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

            builder.Property(x => x.InternalNote)
                .HasMaxLength(2048);

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

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

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

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

            builder.HasMany(x => x.Details)
                .WithOne(x => x.CustomerReconciliationSlip)
                .HasForeignKey(x => x.CustomerReconciliationSlipId)
                .OnDelete(DeleteBehavior.Cascade);

            builder.HasMany(x => x.Logs)
                .WithOne(x => x.CustomerReconciliationSlip)
                .HasForeignKey(x => x.CustomerReconciliationSlipId)
                .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();

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

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

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

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

            builder.HasIndex(x => new { x.CustomerId, x.Status });
        }
    }
}
