from math import radians, sin, cos, sqrt, atan2
from geopy.distance import geodesic

def calculate_distance(lat1, lon1, lat2, lon2):
    """محاسبه فاصله دو نقطه GPS به متر با استفاده از فرمول هاورسین"""
    R = 6371000  # شعاع زمین به متر
    
    lat1_rad = radians(lat1)
    lat2_rad = radians(lat2)
    delta_lat = radians(lat2 - lat1)
    delta_lon = radians(lon2 - lon1)
    
    a = sin(delta_lat / 2) ** 2 + cos(lat1_rad) * cos(lat2_rad) * sin(delta_lon / 2) ** 2
    c = 2 * atan2(sqrt(a), sqrt(1 - a))
    
    return R * c

def verify_gps_location(project_gps_lat, project_gps_lon, user_gps_lat, user_gps_lon, allowed_radius=100):
    """بررسی اینکه کاربر در شعاع مجاز پروژه قرار دارد یا خیر"""
    distance = calculate_distance(project_gps_lat, project_gps_lon, user_gps_lat, user_gps_lon)
    return distance <= allowed_radius, distance