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

public class DigitizationBatchConfiguration : IEntityTypeConfiguration<DigitizationBatch>
{
    public void Configure(EntityTypeBuilder<DigitizationBatch> builder)
    {
        builder.ToTable("digitization_batches", t =>
        {
            t.HasCheckConstraint("chk_batches_status",
                "status IN ('UPLOADED', 'IN_ENTRY', 'PENDING_VERIFICATION', 'REJECTED', 'VERIFIED', 'AWAITING_CLINICAL_APPROVAL', 'APPROVED', 'PROMOTED', 'CANCELLED')");
            t.HasCheckConstraint("chk_batches_batch_type",
                "batch_type IN ('PATIENT_REGISTRATION', 'ENCOUNTER_SUMMARY', 'VITALS_SHEET', 'LAB_RESULTS', 'MEDICATION_LIST', 'ALLERGY_UPDATE', 'MIXED')");
            t.HasCheckConstraint("chk_batches_track",
                "track IN ('BACKFILL', 'LIVE_CAPTURE')");
        });
        builder.HasKey(b => b.Id);
        builder.Property(b => b.Id).HasColumnName("id").HasDefaultValueSql("gen_random_uuid()");
        builder.Property(b => b.Status)
            .HasColumnName("status")
            .HasMaxLength(30)
            .HasConversion(v => v.ToDbString(), v => BatchStatusExtensions.FromDbString(v))
            .HasDefaultValueSql("'UPLOADED'")
            .HasSentinel((BatchStatus)(-1));
        builder.Property(b => b.BatchType)
            .HasColumnName("batch_type")
            .HasMaxLength(30)
            .HasConversion(v => v.ToDbString(), v => BatchTypeExtensions.FromDbString(v))
            .IsRequired();
        builder.Property(b => b.Track)
            .HasColumnName("track")
            .HasMaxLength(20)
            .HasConversion(v => v.ToDbString(), v => BatchTrackExtensions.FromDbString(v))
            .HasDefaultValueSql("'BACKFILL'")
            .HasSentinel((BatchTrack)(-1));
        builder.Property(b => b.PatientId).HasColumnName("patient_id");
        builder.Property(b => b.EncounterDraftId).HasColumnName("encounter_draft_id");
        builder.Property(b => b.DocumentRef).HasColumnName("document_ref").HasMaxLength(500).IsRequired();
        builder.Property(b => b.DocumentSha256).HasColumnName("document_sha256").HasMaxLength(64).IsRequired();
        builder.Property(b => b.EnableRetroactiveAlerts).HasColumnName("enable_retroactive_alerts").HasDefaultValue(false);
        builder.Property(b => b.EnteredByUserId).HasColumnName("entered_by_user_id");
        builder.Property(b => b.VerifiedByUserId).HasColumnName("verified_by_user_id");
        builder.Property(b => b.ApprovedByUserId).HasColumnName("approved_by_user_id");
        builder.Property(b => b.RejectionReason).HasColumnName("rejection_reason");
        builder.Property(b => b.PromotedAt).HasColumnName("promoted_at");
        builder.Property(b => b.PromotionEncounterId).HasColumnName("promotion_encounter_id");
        builder.Property(b => b.SupersedesBatchId).HasColumnName("supersedes_batch_id");
        builder.Property(b => b.ClinicianAttestation).HasColumnName("clinician_attestation").HasDefaultValue(false);
        builder.Property(b => b.CreatedAt).HasColumnName("created_at").HasDefaultValueSql("NOW()");
        builder.Property(b => b.UpdatedAt).HasColumnName("updated_at").HasDefaultValueSql("NOW()");

        builder.HasOne(b => b.EnteredByUser).WithMany().HasForeignKey(b => b.EnteredByUserId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(b => b.VerifiedByUser).WithMany().HasForeignKey(b => b.VerifiedByUserId).OnDelete(DeleteBehavior.Restrict);
        builder.HasOne(b => b.ApprovedByUser).WithMany().HasForeignKey(b => b.ApprovedByUserId).OnDelete(DeleteBehavior.Restrict);

        builder.HasIndex(b => b.Status);
        builder.HasIndex(b => new { b.DocumentSha256, b.PatientId, b.CreatedAt });
        builder.HasIndex(b => b.SupersedesBatchId).HasFilter("supersedes_batch_id IS NOT NULL");
    }
}