#nullable enable
using Baya.Domain.Common;

namespace Baya.Domain.Entities.Booking;

/// <summary>
/// The Electronic Visit Verification (EVV) record — GPS + timestamped check-in/out proving a visit happened;
/// <b>required for payout</b>. The FK is on the <see cref="BookingSessionId"/> (not the booking) so each
/// visit in a multi-session engagement is verified independently. Raw GPS detail is sensitive and gated to
/// the owning nurse + admin only. <see cref="CheckInAddressMatch"/> is <b>advisory</b>: a mismatch flags an
/// admin review alert but never blocks the visit or withholds payout on its own.
/// </summary>
public class VisitVerification : BaseEntity<long>
{
    public long BookingSessionId { get; set; }
    public BookingSession Session { get; set; } = null!;

    public DateTime? CheckInAt { get; set; }
    public decimal? CheckInLat { get; set; }
    public decimal? CheckInLng { get; set; }

    public DateTime? CheckOutAt { get; set; }
    public decimal? CheckOutLat { get; set; }
    public decimal? CheckOutLng { get; set; }

    /// <summary>Advisory: did the check-in fall within <c>evv_location_tolerance_meters</c> of the booking
    /// address? Null when GPS was unavailable at check-in (still allowed, flagged).</summary>
    public bool? CheckInAddressMatch { get; set; }

    /// <summary>The computed check-in distance to the booking address, for the admin review screen.</summary>
    public decimal? CheckInDistanceMeters { get; set; }

    /// <summary>Guarded — mutated only through the transition helpers.</summary>
    public string Status { get; private set; } = VisitVerificationStatus.Pending;

    public DateTimeOffset? DeletedAt { get; set; }

    public void MarkCheckedIn() => Status = VisitVerificationStatus.CheckedIn;

    public void MarkCompleted() => Status = VisitVerificationStatus.Completed;
}
