using Baya.Domain.Entities.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Baya.Infrastructure.Persistence.Configuration.IdentityConfig;

internal sealed class PatientCarePlanConfig : IEntityTypeConfiguration<PatientCarePlan>
{
    public void Configure(EntityTypeBuilder<PatientCarePlan> builder)
    {
        builder.ToTable("PatientCarePlans", "usr");

        // One family-owned plan per patient — the upsert target.
        builder.HasIndex(p => p.PatientId)
            .IsUnique()
            .HasFilter("[DeletedAt] IS NULL")
            .HasDatabaseName("UX_PatientCarePlans_Patient");

        builder.HasOne<Patient>()
            .WithMany()
            .HasForeignKey(p => p.PatientId)
            .IsRequired();

        builder.HasQueryFilter(p => p.DeletedAt == null);
    }
}
