using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Diagnostics.HealthChecks;

public sealed class EncounterReplicaReadyCheck : IHealthCheck
{
    private readonly GatewayDbContext _db;

    public EncounterReplicaReadyCheck(GatewayDbContext db) => _db = db;

    public async Task<HealthCheckResult> CheckHealthAsync(
        HealthCheckContext context, CancellationToken ct = default)
    {
        var encounterCount = await _db.Encounters.CountAsync(ct);
        if (encounterCount > 0)
            return HealthCheckResult.Healthy("Replica data available.");

        var state = await _db.SyncState.FirstOrDefaultAsync(ct);
        if (state?.InitialSyncCompleted == true)
            return HealthCheckResult.Healthy("Initial sync completed.");

        return HealthCheckResult.Unhealthy(
            "No encounter replica data — first boot requires central sync.");
    }
}