// Copyright (C) Call of Nil contributors
// SPDX-License-Identifier: AGPL-3.0-only

use super::Client;
use crate::error::Result;
use crate::http;
use nil_payload::request::city::*;
use nil_payload::response::city::*;

impl Client {
  pub async fn get_city(&self, req: GetCityRequest) -> Result<GetCityResponse> {
    http::json_put("get-city")
      .body(req)
      .server(self.server)
      .maybe_authorization(self.authorization.as_ref())
      .circuit_breaker(self.circuit_breaker())
      .retry(&self.retry)
      .user_agent(&self.user_agent)
      .send()
      .await
  }

  pub async fn get_city_score(&self, req: GetCityScoreRequest) -> Result<GetCityScoreResponse> {
    http::json_put("get-city-score")
      .body(req)
      .server(self.server)
      .circuit_breaker(self.circuit_breaker())
      .retry(&self.retry)
      .user_agent(&self.user_agent)
      .send()
      .await
  }

  pub async fn get_public_cities(
    &self,
    req: GetPublicCitiesRequest,
  ) -> Result<GetPublicCitiesResponse> {
    http::json_put("get-public-cities")
      .body(req)
      .server(self.server)
      .circuit_breaker(self.circuit_breaker())
      .retry(&self.retry)
      .user_agent(&self.user_agent)
      .send()
      .await
  }

  pub async fn get_public_city(&self, req: GetPublicCityRequest) -> Result<GetPublicCityResponse> {
    http::json_put("get-public-city")
      .body(req)
      .server(self.server)
      .circuit_breaker(self.circuit_breaker())
      .retry(&self.retry)
      .user_agent(&self.user_agent)
      .send()
      .await
  }

  pub async fn rename_city(&self, req: RenameCityRequest) -> Result<()> {
    http::post("rename-city")
      .body(req)
      .server(self.server)
      .maybe_authorization(self.authorization.as_ref())
      .circuit_breaker(self.circuit_breaker())
      .user_agent(&self.user_agent)
      .send()
      .await
  }

  pub async fn search_city(&self, req: SearchCityRequest) -> Result<SearchCityResponse> {
    http::json_put("search-city")
      .body(req)
      .server(self.server)
      .maybe_authorization(self.authorization.as_ref())
      .circuit_breaker(self.circuit_breaker())
      .retry(&self.retry)
      .user_agent(&self.user_agent)
      .send()
      .await
  }

  pub async fn search_public_city(
    &self,
    req: SearchPublicCityRequest,
  ) -> Result<SearchPublicCityResponse> {
    http::json_put("search-public-city")
      .body(req)
      .server(self.server)
      .circuit_breaker(self.circuit_breaker())
      .retry(&self.retry)
      .user_agent(&self.user_agent)
      .send()
      .await
  }
}
