profile = "address/ip-geolocation@1.0"
provider = "ipdata"

// IPData documentation: https://docs.ipdata.co/
map IpGeolocation {
  ipAddress = ''

  set if (input.ipAddress) {
    ipAddress = input.ipAddress
  }

  http GET "/{ipAddress}" {
    security "apikey"
    
    request {
      query {
        fields = 'ip,country_code,country_name,region,city,postal,time_zone,latitude,longitude'
      }
    }

    response 200 "application/json" {
      timezone = undefined

      set if (body.time_zone) {
        timezone = body.time_zone.name
      }

      map result {
        ipAddress = body.ip
        addressCountryCode = body.country_code ? body.country_code : undefined
        addressCountry = body.country_name ? body.country_name : undefined
        addressRegion = body.region ? body.region : undefined
        addressLocality = body.city ? body.city: undefined
        postalCode = body.postal ? body.postal : undefined
        timeZone = timezone
        latitude = body.latitude ? body.latitude : undefined
        longitude = body.longitude ? body.longitude : undefined
      }
    }

    response {
      error = call MapError(statusCode = statusCode, body = body)
      map error error
    }
  }
}

//Status codes documentation: https://docs.ipdata.co/api-reference/status-codes
operation MapError {
  statusCode = args.statusCode
  body = args.body
  detail = body.message

  return if (statusCode === 400) {
    title = "Bad request"
    detail = detail
  }

  return if (statusCode === 401) {
    title = "Unauthenticated"
    detail = detail
  }

  return if (statusCode === 403) {
    title = "Unauthorized"
    detail = detail
  }

  return if (statusCode === 404) {
    title = "Not found"
    detail = detail
  }

  return {
    title = "Unknown error"
    detail = `Unknown error occurred. Status: ${statusCode}. IPData provider error info: ${detail}.`
  }
}