using CMSMicroservice.Domain.Entities.Order;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace CMSMicroservice.Infrastructure.Persistence.Configurations;

public class OrderVATConfiguration : IEntityTypeConfiguration<OrderVAT>
{
    public void Configure(EntityTypeBuilder<OrderVAT> builder)
    {
        builder.HasQueryFilter(p => !p.IsDeleted);
        builder.ToTable("OrderVATs");

        builder.HasKey(x => x.Id);

        builder.Property(x => x.OrderId)
            .IsRequired();

        builder.Property(x => x.VATRate)
            .IsRequired()
            .HasColumnType("decimal(5,4)"); // 0.0900 (9%)

        builder.Property(x => x.BaseAmount)
            .IsRequired();

        builder.Property(x => x.VATAmount)
            .IsRequired();

        builder.Property(x => x.TotalAmount)
            .IsRequired();

        builder.Property(x => x.IsPaid)
            .IsRequired()
            .HasDefaultValue(false);

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

        // Foreign Key
        builder.HasOne(x => x.Order)
            .WithOne(x => x.OrderVAT)
            .HasForeignKey<OrderVAT>(x => x.OrderId)
            .OnDelete(DeleteBehavior.Restrict);

        // Indexes
        builder.HasIndex(x => x.OrderId)
            .IsUnique()
            .HasDatabaseName("IX_OrderVATs_OrderId");

        builder.HasIndex(x => x.IsPaid)
            .HasDatabaseName("IX_OrderVATs_IsPaid");

        builder.HasIndex(x => x.Created)
            .HasDatabaseName("IX_OrderVATs_Created");
    }
}
