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

public class SimulationRunConfiguration : IEntityTypeConfiguration<SimulationRun>
{
    public void Configure(EntityTypeBuilder<SimulationRun> builder)
    {
        builder.ToTable("simulation_runs");
        builder.HasKey(r => r.Id);
        builder.Property(r => r.Id).HasColumnName("id").HasDefaultValueSql("gen_random_uuid()");
        builder.Property(r => r.ScenarioId).HasColumnName("scenario_id").HasMaxLength(100).IsRequired();
        builder.Property(r => r.ScenarioName).HasColumnName("scenario_name").HasMaxLength(200).IsRequired();
        builder.Property(r => r.Speed).HasColumnName("speed");
        builder.Property(r => r.Status).HasColumnName("status").HasMaxLength(20).IsRequired()
            .HasConversion(
                v => v.ToDbString(),
                v => SimulationRunStatusExtensions.FromDbString(v));
        builder.Property(r => r.PatientId).HasColumnName("patient_id");
        builder.Property(r => r.EncounterId).HasColumnName("encounter_id");
        builder.Property(r => r.StartedByUserId).HasColumnName("started_by_user_id").HasMaxLength(100).IsRequired();
        builder.Property(r => r.StartedAt).HasColumnName("started_at");
        builder.Property(r => r.CompletedAt).HasColumnName("completed_at");
        builder.Property(r => r.ObservationsSent).HasColumnName("observations_sent");
        builder.Property(r => r.MedicationsSent).HasColumnName("medications_sent");
        builder.Property(r => r.OrdersPlaced).HasColumnName("orders_placed");
        builder.Property(r => r.LastOffsetMinutes).HasColumnName("last_offset_minutes");
        builder.Property(r => r.TotalOffsetMinutes).HasColumnName("total_offset_minutes");
        builder.Property(r => r.FailureReason).HasColumnName("failure_reason").HasMaxLength(2000);

        builder.HasIndex(r => new { r.Status, r.StartedAt })
            .IsDescending(false, true)
            .HasDatabaseName("IX_simulation_runs_status_started_at");
    }
}
