/**
 * IP 归属地查询工具
 * 通过系统配置中的 IP 地理接口查询
 */

import { getSystemConfig } from '../admin/system-config.service';

export async function lookupIpLocation(ip: string): Promise<string | null> {
  if (!ip || ip === '127.0.0.1' || ip === '::1' || ip.startsWith('192.168.') || ip.startsWith('10.')) {
    return null;
  }
  try {
    const apiUrlTemplate = getSystemConfig('ip_geo_api_url');
    if (!apiUrlTemplate) return null;
    const url = apiUrlTemplate.replace('{ip}', encodeURIComponent(ip));

    const res = await fetch(url, { signal: AbortSignal.timeout(5000) });
    if (!res.ok) return null;
    const data = await res.json() as {
      code: number; sheng?: string; shi?: string; qu?: string;
      isp?: string; msg?: string; guo?: string;
    };
    if (data.code !== 200) return null;
    // Format: "四川 绵阳 江油 中国联通" — strip 省/市/区/州 suffixes for compact display
    const stripSuffix = (s: string | undefined) => s?.replace(/[省市州区]$/, '');
    const parts = [stripSuffix(data.sheng), stripSuffix(data.shi), stripSuffix(data.qu), data.isp].filter(Boolean);
    return parts.join(' ');
  } catch {
    return null;
  }
}
