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

public class SofaScoreConfiguration : IEntityTypeConfiguration<SofaScore>
{
    public void Configure(EntityTypeBuilder<SofaScore> builder)
    {
        builder.ToTable("sofa_scores");
        builder.HasKey(s => s.Id);
        builder.Property(s => s.Id).HasColumnName("id").HasDefaultValueSql("gen_random_uuid()");
        builder.Property(s => s.EncounterId).HasColumnName("encounter_id").IsRequired();
        builder.Property(s => s.PatientId).HasColumnName("patient_id").IsRequired();
        builder.Property(s => s.TotalScore).HasColumnName("total_score").IsRequired();
        builder.Property(s => s.RespiratoryScore).HasColumnName("respiratory_score").IsRequired();
        builder.Property(s => s.CoagulationScore).HasColumnName("coagulation_score").IsRequired();
        builder.Property(s => s.LiverScore).HasColumnName("liver_score").IsRequired();
        builder.Property(s => s.CardiovascularScore).HasColumnName("cardiovascular_score").IsRequired();
        builder.Property(s => s.CnsScore).HasColumnName("cns_score").IsRequired();
        builder.Property(s => s.RenalScore).HasColumnName("renal_score").IsRequired();
        builder.Property(s => s.IsBaseline).HasColumnName("is_baseline").IsRequired();
        builder.Property(s => s.DeltaFromBaseline).HasColumnName("delta_from_baseline");
        builder.Property(s => s.StalenessFlags).HasColumnName("staleness_flags").HasColumnType("jsonb");
        builder.Property(s => s.CalculatedAt).HasColumnName("calculated_at").IsRequired();
        builder.Property(s => s.CreatedAt).HasColumnName("created_at").IsRequired();

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

        builder.HasIndex(s => new { s.EncounterId, s.CalculatedAt });
        builder.HasIndex(s => s.EncounterId)
            .HasFilter("is_baseline = true")
            .HasDatabaseName("idx_sofa_scores_baseline");
    }
}