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

namespace Lada.Framework.Data.Mappings.EntityMappings.Finances.Wallets
{
    public class TransactionMap : IEntityMappingConfiguration<Transaction>
    {
        public void Map(EntityTypeBuilder<Transaction> builder)
        {
            builder.HasKey(a => a.Id);
            builder.Property(t => t.Description)
                    .HasMaxLength(1024);

            builder.HasOne(t => t.Wallet)
            .WithMany(w => w.Transactions)
            .HasForeignKey(t => t.WalletId)
            .OnDelete(DeleteBehavior.Restrict);

            builder.HasOne(t => t.RelatedWallet)
                .WithMany(w => w.RelatedTransactions)
                .HasForeignKey(t => t.RelatedWalletId)
                .OnDelete(DeleteBehavior.Restrict);

            builder.HasOne(t => t.Expense)
                .WithMany()
                .HasForeignKey(t => t.ExpenseId)
                .OnDelete(DeleteBehavior.SetNull);

            builder.HasOne(t => t.Income)
                .WithMany()
                .HasForeignKey(t => t.IncomeId)
                .OnDelete(DeleteBehavior.SetNull);

            //// Nếu có liên kết với Order
            //builder.Property(t => t.OrderId)
            //    .IsRequired(false);

            builder.ToTable("Transaction");
        }
    }

}
