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

public class EncounterConfiguration : IEntityTypeConfiguration<Encounter>
{
    public void Configure(EntityTypeBuilder<Encounter> builder)
    {
        builder.ToTable("encounters", "clinical", t =>
        {
            t.HasCheckConstraint("chk_encounters_department",
                "department IS NULL OR department IN ('Emergency Department', 'Internal Medicine', 'General Medicine', 'Surgery', 'ICU', 'NICU', 'Medical-Surgical', 'Outpatient Clinic', 'Pediatrics', 'Obstetrics & Gynecology', 'Labor & Delivery', 'Cardiology', 'Orthopedics', 'Neurology', 'Oncology', 'Radiology', 'Laboratory', 'Psychiatry', 'Physical Therapy', 'Anesthesiology')");
        });
        builder.HasKey(e => e.Id);
        builder.Property(e => e.Id).HasColumnName("id").HasDefaultValueSql("gen_random_uuid()");
        builder.Property(e => e.PatientId).HasColumnName("patient_id").IsRequired();
        builder.Property(e => e.AdmissionDate).HasColumnName("admission_date");
        builder.Property(e => e.Department)
            .HasColumnName("department")
            .HasMaxLength(100)
            .HasConversion(
                v => v.HasValue ? v.Value.ToDbString() : null,
                v => v == null ? null : DepartmentExtensions.FromDbString(v));
        builder.Property(e => e.RoomBed).HasColumnName("room_bed").HasMaxLength(50);
        builder.Property(e => e.AdmissionReason).HasColumnName("admission_reason").HasMaxLength(500);
        builder.Property(e => e.DischargeDiagnosis).HasColumnName("discharge_diagnosis").HasMaxLength(500);
        builder.Property(e => e.Status).HasColumnName("status").HasMaxLength(20).IsRequired();
        builder.Property(e => e.SourceBatchId).HasColumnName("source_batch_id");
        builder.Property(e => e.CreatedAt).HasColumnName("created_at").HasDefaultValueSql("NOW()");
        builder.Property(e => e.UpdatedAt).HasColumnName("updated_at").HasDefaultValueSql("NOW()");

        builder.HasOne(e => e.Patient).WithMany().HasForeignKey(e => e.PatientId).OnDelete(DeleteBehavior.Restrict);
        builder.HasIndex(e => new { e.PatientId, e.Status }).HasDatabaseName("ix_encounters_patient_status");
    }
}