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

public class LiveObservationConfiguration : IEntityTypeConfiguration<LiveObservation>
{
    public void Configure(EntityTypeBuilder<LiveObservation> builder)
    {
        builder.ToTable("live_observations");
        builder.HasKey(o => o.Id);
        builder.Property(o => o.Id).HasColumnName("id").HasDefaultValueSql("gen_random_uuid()");
        builder.Property(o => o.EncounterId).HasColumnName("encounter_id").IsRequired();
        builder.Property(o => o.PatientId).HasColumnName("patient_id");
        builder.Property(o => o.SourceBatchId).HasColumnName("source_batch_id").IsRequired();
        builder.Property(o => o.ObservationCode).HasColumnName("observation_code").HasMaxLength(50).IsRequired();
        builder.Property(o => o.Value).HasColumnName("value").HasColumnType("decimal(10,3)").IsRequired();
        builder.Property(o => o.Unit).HasColumnName("unit").HasMaxLength(20).IsRequired();
        builder.Property(o => o.RecordedAt).HasColumnName("recorded_at").IsRequired();
        builder.Property(o => o.Note).HasColumnName("note").HasMaxLength(500);
        builder.Property(o => o.CreatedAt).HasColumnName("created_at").HasDefaultValueSql("NOW()");

        // Phase 5: Supersession columns
        builder.Property(o => o.IsSuperseded).HasColumnName("is_superseded").HasDefaultValue(false);
        builder.Property(o => o.SupersededByBatchId).HasColumnName("superseded_by_batch_id");
        builder.Property(o => o.SupersededAt).HasColumnName("superseded_at");

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

        builder.HasIndex(o => new { o.EncounterId, o.ObservationCode });
        builder.HasIndex(o => o.SourceBatchId);
        builder.HasIndex(o => o.IsSuperseded).HasFilter("is_superseded = true");
    }
}