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

namespace Lada.Framework.Data.Mappings.EntityMappings.Crm
{
    public class CustomerBankAccountMap : IEntityMappingConfiguration<CustomerBankAccount>
    {
        public void Map(EntityTypeBuilder<CustomerBankAccount> builder)
        {
            builder.ToTable("CustomerBankAccounts");

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

            // Properties
            builder.Property(x => x.AccountNumber)
                .HasMaxLength(64)
                .IsRequired();

            builder.Property(x => x.AccountHolderName)
                .HasMaxLength(256)
                .IsRequired();

            builder.Property(x => x.BankCode)
                .HasMaxLength(16)
                .IsRequired();

            builder.Property(x => x.BankName)
                .HasMaxLength(256)
                .IsRequired();

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

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

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

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

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

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

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

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