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

namespace Lada.Framework.Data.Mappings.EntityMappings.Finances
{
    public class BankAccountMap : IEntityMappingConfiguration<BankAccount>
    {
        public void Map(EntityTypeBuilder<BankAccount> builder)
        {
            builder.HasKey(a => a.Id);
            builder.Property(a => a.Id).ValueGeneratedOnAdd();

            // Properties
            builder.Property(a => a.AccountName)
                .HasMaxLength(256)
                .IsRequired();

            builder.Property(a => a.AccountNumber)
                .HasMaxLength(50)
                .IsRequired();


            // Indexes
            builder.HasIndex(a => a.AccountNumber)
                .HasDatabaseName("IX_BankAccount_AccountNumber");


            // Relationships
            builder.HasOne(a => a.Bank)
                .WithMany(b => b.BankAccounts)
                .HasForeignKey(a => a.BankId)
                .OnDelete(DeleteBehavior.SetNull);

            builder.HasOne(a => a.ManagerUser)
                .WithMany()
                .HasForeignKey(a => a.ManagerUserId)
                .OnDelete(DeleteBehavior.SetNull);

            builder.HasMany(a => a.BankTransactions)
                .WithOne(t => t.BankAccount)
                .HasForeignKey(t => t.BankAccountId)
                .OnDelete(DeleteBehavior.Cascade);

            builder.HasMany(a => a.BankStatementImports)
                .WithOne(i => i.BankAccount)
                .HasForeignKey(i => i.BankAccountId)
                .OnDelete(DeleteBehavior.Cascade);

            builder.ToTable("BankAccount");
        }
    }
}
