using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

public class SepsisBundleConfiguration : IEntityTypeConfiguration<SepsisBundle>
{
    public void Configure(EntityTypeBuilder<SepsisBundle> builder)
    {
        builder.ToTable("sepsis_bundles", t =>
        {
            t.HasCheckConstraint("chk_sepsis_bundles_compliance_status",
                "compliance_status IN ('IN_PROGRESS', 'COMPLIANT', 'NON_COMPLIANT')");
        });

        builder.HasKey(b => b.Id);
        builder.Property(b => b.Id).HasColumnName("id").HasDefaultValueSql("gen_random_uuid()");
        builder.Property(b => b.EncounterId).HasColumnName("encounter_id").IsRequired();
        builder.Property(b => b.TriggeringAlertId).HasColumnName("triggering_alert_id").IsRequired();
        builder.Property(b => b.TriggeringAlertType)
            .HasColumnName("triggering_alert_type")
            .HasMaxLength(50)
            .IsRequired();
        builder.Property(b => b.RecognizedAt).HasColumnName("recognized_at").IsRequired();
        builder.Property(b => b.DeadlineAt).HasColumnName("deadline_at").IsRequired();
        builder.Property(b => b.ComplianceStatus)
            .HasColumnName("compliance_status")
            .HasMaxLength(20)
            .HasConversion(
                v => v.ToDbString(),
                v => SepsisBundleComplianceStatusExtensions.FromDbString(v))
            .HasDefaultValueSql("'IN_PROGRESS'")
            .HasSentinel((SepsisBundleComplianceStatus)(-1));
        builder.Property(b => b.CompletedAt).HasColumnName("completed_at");

        builder.HasOne(b => b.Encounter)
            .WithMany()
            .HasForeignKey(b => b.EncounterId)
            .OnDelete(DeleteBehavior.Restrict);

        builder.HasOne(b => b.TriggeringAlert)
            .WithMany()
            .HasForeignKey(b => b.TriggeringAlertId)
            .OnDelete(DeleteBehavior.Restrict);

        builder.HasIndex(b => new { b.EncounterId, b.RecognizedAt });
        builder.HasIndex(b => b.ComplianceStatus);
    }
}
