use actix_web::{delete, get, put, web, HttpResponse};
use emgauwa_lib::db::DbController;
use emgauwa_lib::errors::{DatabaseError, EmgauwaError};
use emgauwa_lib::models::{convert_db_list, Controller, FromDbModel};
use emgauwa_lib::types::ControllerUid;
use serde_derive::{Deserialize, Serialize};
use sqlx::{Pool, Sqlite};

#[derive(Debug, Serialize, Deserialize)]
pub struct RequestController {
	name: String,
}

#[get("/api/v1/controllers")]
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, EmgauwaError> {
	let mut pool_conn = pool.acquire().await?;

	let db_controllers = DbController::get_all(&mut pool_conn).await?;

	let controllers: Vec<Controller> = convert_db_list(&mut pool_conn, db_controllers)?;

	Ok(HttpResponse::Ok().json(controllers))
}

#[get("/api/v1/controllers/{controller_id}")]
pub async fn show(
	pool: web::Data<Pool<Sqlite>>,
	path: web::Path<(String,)>,
) -> Result<HttpResponse, EmgauwaError> {
	let mut pool_conn = pool.acquire().await?;

	let (controller_uid,) = path.into_inner();
	let uid = ControllerUid::try_from(controller_uid.as_str())?;

	let controller = DbController::get_by_uid(&mut pool_conn, &uid)
		.await?
		.ok_or(DatabaseError::NotFound)?;

	let return_controller = Controller::from_db_model(&mut pool_conn, controller)?;
	Ok(HttpResponse::Ok().json(return_controller))
}

#[put("/api/v1/controllers/{controller_id}")]
pub async fn update(
	pool: web::Data<Pool<Sqlite>>,
	path: web::Path<(String,)>,
	data: web::Json<RequestController>,
) -> Result<HttpResponse, EmgauwaError> {
	let mut pool_conn = pool.acquire().await?;

	let (controller_uid,) = path.into_inner();
	let uid = ControllerUid::try_from(controller_uid.as_str())?;

	let controller = DbController::get_by_uid(&mut pool_conn, &uid)
		.await?
		.ok_or(DatabaseError::NotFound)?;

	let controller = controller
		.update(&mut pool_conn, data.name.as_str(), controller.relay_count)
		.await?;

	let return_controller = Controller::from_db_model(&mut pool_conn, controller)?;
	Ok(HttpResponse::Ok().json(return_controller))
}

#[delete("/api/v1/controllers/{controller_id}")]
pub async fn delete(
	pool: web::Data<Pool<Sqlite>>,
	path: web::Path<(String,)>,
) -> Result<HttpResponse, EmgauwaError> {
	let mut pool_conn = pool.acquire().await?;

	let (controller_uid,) = path.into_inner();
	let uid = ControllerUid::try_from(controller_uid.as_str())?;

	DbController::delete_by_uid(&mut pool_conn, uid).await?;
	Ok(HttpResponse::Ok().json("controller got deleted"))
}