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

namespace OmsorgCore.Infrastructure.Persistence.Configurations;

public class EmployeeFacilityDistanceConfiguration : IEntityTypeConfiguration<EmployeeFacilityDistance>
{
    public void Configure(EntityTypeBuilder<EmployeeFacilityDistance> builder)
    {
        builder.ToTable("employee_facility_distances");
        builder.HasKey(d => d.Id);
        builder.Property(d => d.DistanceKm).IsRequired().HasColumnType("decimal(10,2)");

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

        builder.HasOne(d => d.Employee)
            .WithMany()
            .HasForeignKey(d => d.EmployeeId)
            .OnDelete(DeleteBehavior.Restrict);

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