using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using OmsorgCore.Domain.Entities;

namespace OmsorgCore.Infrastructure.Persistence.Configurations;

public class InvoiceConfiguration : IEntityTypeConfiguration<Invoice>
{
    public void Configure(EntityTypeBuilder<Invoice> builder)
    {
        builder.ToTable("invoices");
        builder.HasKey(i => i.Id);
        builder.Property(i => i.InvoiceNumber).IsRequired().HasMaxLength(50);
        builder.HasIndex(i => i.InvoiceNumber).IsUnique();
        builder.Property(i => i.Status).IsRequired().HasMaxLength(50);
        builder.Property(i => i.NetAmount).HasColumnType("numeric(12,2)");
        builder.Property(i => i.GrossAmount).HasColumnType("numeric(12,2)");

        builder.HasOne(i => i.Facility)
            .WithMany()
            .HasForeignKey(i => i.FacilityId)
            .OnDelete(DeleteBehavior.Restrict);

        // Soft-gelöschte Rechnungen sind für alle normalen Queries unsichtbar.
        builder.HasQueryFilter(i => !i.IsDeleted);
    }
}
