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

use crate::app::App;
use crate::middleware::authorization::CurrentPlayer;
use crate::response::from_err;
use crate::{bail_if_city_is_not_owned_by, bail_if_player_is_not_pending, res};
use axum::extract::{Extension, Json, State};
use axum::response::Response;
use nil_core::infrastructure::building::r#impl::academy::recruit_catalog::AcademyRecruitCatalog;
use nil_payload::request::infrastructure::academy::*;
use nil_payload::response::infrastructure::academy::*;

pub async fn add_recruit_order(
  State(app): State<App>,
  Extension(player): Extension<CurrentPlayer>,
  Json(req): Json<AddAcademyRecruitOrderRequest>,
) -> Response {
  match app.get(req.world) {
    Ok(world) => {
      let result = try {
        let mut world = world.write().await;
        bail_if_player_is_not_pending!(world, &player.0);
        bail_if_city_is_not_owned_by!(world, &player.0, req.request.coord);
        world.add_academy_recruit_order(&req.request)?;
      };

      result
        .map(|()| res!(OK))
        .unwrap_or_else(from_err)
    }
    Err(err) => from_err(err),
  }
}

pub async fn cancel_recruit_order(
  State(app): State<App>,
  Extension(player): Extension<CurrentPlayer>,
  Json(req): Json<CancelAcademyRecruitOrderRequest>,
) -> Response {
  match app.get(req.world) {
    Ok(world) => {
      let result = try {
        let mut world = world.write().await;
        bail_if_player_is_not_pending!(world, &player.0);
        bail_if_city_is_not_owned_by!(world, &player.0, req.coord);
        world.cancel_academy_recruit_order(req.coord, req.id)?;
      };

      result
        .map(|()| res!(OK))
        .unwrap_or_else(from_err)
    }
    Err(err) => from_err(err),
  }
}

pub async fn get_recruit_catalog(
  State(app): State<App>,
  Extension(player): Extension<CurrentPlayer>,
  Json(req): Json<GetAcademyRecruitCatalogRequest>,
) -> Response {
  match app.get(req.world) {
    Ok(world) => {
      let result = try {
        let world = world.read().await;
        bail_if_city_is_not_owned_by!(world, &player.0, req.coord);
        let infra = world.city(req.coord)?.infrastructure();
        AcademyRecruitCatalog::new(infra)
      };

      result
        .map(|catalog| res!(OK, GetAcademyRecruitCatalogResponse(catalog)))
        .unwrap_or_else(from_err)
    }
    Err(err) => from_err(err),
  }
}
