using Microsoft.EntityFrameworkCore;

/// <summary>
/// Seeds promoted clinical records aligned with Phase 9 demo batches for FHIR tests.
/// DataSeeder creates digitization batches; this helper populates the clinical tables
/// that FhirService reads.
/// </summary>
public static class FhirClinicalSeedHelper
{
    public static readonly Guid Patient1Id = Guid.Parse("b1000000-0000-0000-0000-000000000001");
    public static readonly Guid Patient2Id = Guid.Parse("b1000000-0000-0000-0000-000000000002");
    public static readonly Guid Encounter1Id = Guid.Parse("d1000000-0000-0000-0000-000000000001");
    public static readonly Guid HeartRateObsId = Guid.Parse("e1000000-0000-0000-0000-000000000001");
    public static readonly Guid WbcObsId = Guid.Parse("e1000000-0000-0000-0000-000000000002");
    public static readonly Guid Batch1Id = Guid.Parse("c1000000-0000-0000-0000-000000000001");

    public static async Task SeedAsync(AppDbContext db)
    {
        if (await db.Patients.AnyAsync())
            return;

        var now = DateTimeOffset.UtcNow;
        var recordedAt = now.AddDays(-5);

        db.Patients.AddRange(
            new Patient
            {
                Id = Patient1Id,
                Mrn = "VCR-000001",
                FullName = "MARIA SANTOS",
                DateOfBirth = new DateOnly(1978, 3, 15),
                Sex = "female",
                BloodType = BloodType.APos,
                EmergencyContact = "Juan Santos - 555-0101",
                NoKnownAllergies = false,
                AllergiesJson = "[\"Penicillin\", \"Sulfa drugs\"]",
                CreatedAt = now.AddDays(-3),
                UpdatedAt = now.AddHours(-12),
            },
            new Patient
            {
                Id = Patient2Id,
                Mrn = "VCR-000002",
                FullName = "KENJI NAKAMURA",
                DateOfBirth = new DateOnly(1952, 11, 8),
                Sex = "male",
                BloodType = BloodType.ONeg,
                EmergencyContact = "Yuki Nakamura - 555-0202",
                NoKnownAllergies = true,
                CreatedAt = now.AddDays(-1),
                UpdatedAt = now.AddDays(-1),
            });

        db.Encounters.Add(new Encounter
        {
            Id = Encounter1Id,
            PatientId = Patient1Id,
            AdmissionDate = recordedAt,
            Department = Department.InternalMedicine,
            RoomBed = "2A-04",
            AdmissionReason = "Pneumonia with elevated WBC",
            Status = "active",
            SourceBatchId = Batch1Id,
            CreatedAt = now.AddDays(-3),
            UpdatedAt = now.AddHours(-12),
        });

        db.Observations.AddRange(
            new Observation
            {
                Id = HeartRateObsId,
                EncounterId = Encounter1Id,
                PatientId = Patient1Id,
                ObservationCode = "HEART_RATE",
                Value = 88m,
                Unit = "bpm",
                RecordedAt = recordedAt,
                Source = "digitization_backfill",
                SourceBatchId = Batch1Id,
                CreatedAt = now.AddHours(-12),
            },
            new Observation
            {
                Id = WbcObsId,
                EncounterId = Encounter1Id,
                PatientId = Patient1Id,
                ObservationCode = "WBC_K_UL",
                Value = 14.2m,
                Unit = "K/uL",
                RecordedAt = recordedAt,
                Source = "digitization_backfill",
                SourceBatchId = Batch1Id,
                CreatedAt = now.AddHours(-12),
            });

        await db.SaveChangesAsync();
    }
}
