using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using StackExchange.Redis;

public static class GatewayTestSeeder
{
    public static async Task<(Guid PatientId, Guid EncounterId)> SeedActiveEncounterAsync(
        IServiceProvider services)
    {
        using var scope = services.CreateScope();
        var db = scope.ServiceProvider.GetRequiredService<GatewayDbContext>();
        await GatewayDbResetHelper.ResetAsync(db);

        var now = DateTimeOffset.UtcNow;
        var patient = new ReplicaPatient
        {
            Id = Guid.NewGuid(),
            Mrn = "MRN-GW-001",
            FirstName = "Gateway",
            LastName = "Test",
            DateOfBirth = new DateOnly(1970, 1, 1),
            Gender = "F",
            SyncedAt = now
        };
        var encounter = new ReplicaEncounter
        {
            Id = Guid.NewGuid(),
            PatientId = patient.Id,
            EncounterType = EncounterType.Inpatient,
            Status = EncounterStatus.Active,
            Department = Department.Icu,
            AttendingPhysician = "Dr. Gateway",
            RoomBed = "ICU-01",
            AdmittedAt = now,
            SyncedAt = now
        };
        db.Patients.Add(patient);
        db.Encounters.Add(encounter);
        db.AlertThresholds.Add(new ReplicaAlertThreshold
        {
            Id = Guid.NewGuid(),
            ObservationCode = "HEART_RATE",
            DisplayName = "Heart Rate",
            Unit = "bpm",
            CriticalLow = 30,
            WarningLow = 50,
            WarningHigh = 100,
            CriticalHigh = 150,
            SyncedAt = now
        });
        db.SyncState.Add(new GatewaySyncState
        {
            Id = Guid.NewGuid(),
            InitialSyncCompleted = true,
            LastEncounterSyncAt = now
        });
        await db.SaveChangesAsync();

        var redis = scope.ServiceProvider.GetRequiredService<IConnectionMultiplexer>();
        var cache = redis.GetDatabase();
        await cache.StringSetAsync(
            "threshold:HEART_RATE",
            JsonSerializer.Serialize(new ThresholdCacheEntry(
                "HEART_RATE", 30, 50, 100, 150)));

        return (patient.Id, encounter.Id);
    }
}
