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 TaxInvoiceMap : IEntityMappingConfiguration<TaxInvoice>
    {
        public void Map(EntityTypeBuilder<TaxInvoice> builder)
        {
            builder.ToTable("TaxInvoices");

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

            // Properties
            builder.Property(x => x.InvoiceNumber)
                .HasMaxLength(50);

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

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

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

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

            builder.Property(x => x.VatRate)
                .HasPrecision(5, 2);

            // Relationships
            builder.HasOne(x => x.CustomerReconciliationSlip)
                .WithOne(x => x.TaxInvoice)
                .HasForeignKey<TaxInvoice>(x => x.CustomerReconciliationSlipId)
                .OnDelete(DeleteBehavior.Restrict);

            builder.HasOne(x => x.VatInfo)
                .WithMany()
                .HasForeignKey(x => x.VatInfoId)
                .OnDelete(DeleteBehavior.Restrict);

            builder.HasOne(x => x.Media)
                .WithMany()
                .HasForeignKey(x => x.MediaId)
                .OnDelete(DeleteBehavior.SetNull);

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

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

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

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

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