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

namespace OmsorgCore.Infrastructure.Persistence.Configurations;

public class OrderConfiguration : IEntityTypeConfiguration<Order>
{
    public void Configure(EntityTypeBuilder<Order> builder)
    {
        builder.ToTable("orders");
        builder.HasKey(o => o.Id);
        builder.Property(o => o.RequiredQualification).HasMaxLength(200);
        builder.Property(o => o.ShiftType).HasMaxLength(100);
        builder.Property(o => o.Conditions).HasMaxLength(500);
        builder.Property(o => o.Priority).IsRequired().HasMaxLength(50);

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

        builder.HasOne(o => o.FacilityContact)
            .WithMany()
            .HasForeignKey(o => o.FacilityContactId)
            .OnDelete(DeleteBehavior.Restrict);

        builder.HasOne(o => o.Status)
            .WithMany()
            .HasForeignKey(o => o.StatusId)
            .OnDelete(DeleteBehavior.Restrict);

        // Soft-gelöschte Aufträge sind für alle normalen Queries unsichtbar (analog Facility/Contract).
        builder.HasQueryFilter(o => !o.IsDeleted);
    }
}
