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

public class QsofaEvaluationConfiguration : IEntityTypeConfiguration<QsofaEvaluation>
{
    public void Configure(EntityTypeBuilder<QsofaEvaluation> builder)
    {
        builder.ToTable("qsofa_evaluations", t =>
        {
            t.HasCheckConstraint("chk_qsofa_evaluations_active_criteria",
                "active_criteria >= 0 AND active_criteria <= 3");
        });
        builder.HasKey(e => e.Id);
        builder.Property(e => e.Id).HasColumnName("id").HasDefaultValueSql("gen_random_uuid()");
        builder.Property(e => e.EncounterId).HasColumnName("encounter_id").IsRequired();
        builder.Property(e => e.PatientId).HasColumnName("patient_id").IsRequired();
        builder.Property(e => e.ActiveCriteria).HasColumnName("active_criteria").IsRequired();
        builder.Property(e => e.RespRate).HasColumnName("resp_rate");
        builder.Property(e => e.SystolicBp).HasColumnName("systolic_bp");
        builder.Property(e => e.Avpu).HasColumnName("avpu");
        builder.Property(e => e.ScreenAlertFired).HasColumnName("screen_alert_fired").IsRequired();
        builder.Property(e => e.EvaluatedAt).HasColumnName("evaluated_at").IsRequired();
        builder.Property(e => e.CreatedAt).HasColumnName("created_at").IsRequired();

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

        builder.HasIndex(e => new { e.EncounterId, e.EvaluatedAt });
    }
}
