// FE-PAGE-012: Complete Sessions page implementation - IP Location

/**
 * Get location information from IP address
 * Note: This is a simplified implementation. In production, you might want to use
 * a service like ipapi.co, ip-api.com, or MaxMind GeoIP2
 */
export interface LocationInfo {
  country?: string;
  region?: string;
  city?: string;
  isp?: string;
}

/**
 * Format location info for display
 */
export function formatLocation(location: LocationInfo | null): string {
  if (!location) return 'Unknown location';

  const parts: string[] = [];
  if (location.city) parts.push(location.city);
  if (location.region) parts.push(location.region);
  if (location.country) parts.push(location.country);

  return parts.length > 0 ? parts.join(', ') : 'Unknown location';
}

/**
 * Check if IP is localhost/private
 */
export function isPrivateIP(ip: string): boolean {
  return (
    ip === '127.0.0.1' ||
    ip === 'localhost' ||
    ip.startsWith('192.168.') ||
    ip.startsWith('10.') ||
    ip.startsWith('172.16.') ||
    ip.startsWith('172.17.') ||
    ip.startsWith('172.18.') ||
    ip.startsWith('172.19.') ||
    ip.startsWith('172.20.') ||
    ip.startsWith('172.21.') ||
    ip.startsWith('172.22.') ||
    ip.startsWith('172.23.') ||
    ip.startsWith('172.24.') ||
    ip.startsWith('172.25.') ||
    ip.startsWith('172.26.') ||
    ip.startsWith('172.27.') ||
    ip.startsWith('172.28.') ||
    ip.startsWith('172.29.') ||
    ip.startsWith('172.30.') ||
    ip.startsWith('172.31.')
  );
}

/**
 * Get location info from IP (mock implementation)
 * In production, this would call an external API
 */
export async function getLocationFromIP(
  ip: string,
): Promise<LocationInfo | null> {
  if (isPrivateIP(ip)) {
    return {
      country: 'Local',
      region: 'Network',
      city: 'Private IP',
    };
  }

  // TODO: Implement actual IP geolocation API call
  // For now, return null to indicate location is not available
  // Example implementation:
  // try {
  //   const response = await fetch(`https://ipapi.co/${ip}/json/`);
  //   const data = await response.json();
  //   return {
  //     country: data.country_name,
  //     region: data.region,
  //     city: data.city,
  //     isp: data.org,
  //   };
  // } catch (error) {
  //   console.error('Failed to get location from IP:', error);
  //   return null;
  // }

  return null;
}
