namespace Baya.Application.Common;

/// <summary>
/// Great-circle distance between two lat/lng points, in metres (haversine). Used by the EVV check-in
/// address-match: the nurse's captured GPS is compared against the booking address coordinates, and the
/// result is <b>advisory only</b> — a mismatch flags admin review, never blocks the visit.
/// </summary>
public static class GeoDistance
{
    private const double EarthRadiusMeters = 6_371_000d;

    public static double HaversineMeters(double lat1, double lng1, double lat2, double lng2)
    {
        var dLat = ToRadians(lat2 - lat1);
        var dLng = ToRadians(lng2 - lng1);

        var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2)
                + Math.Cos(ToRadians(lat1)) * Math.Cos(ToRadians(lat2))
                  * Math.Sin(dLng / 2) * Math.Sin(dLng / 2);

        var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
        return EarthRadiusMeters * c;
    }

    private static double ToRadians(double degrees) => degrees * Math.PI / 180d;
}
