use actix::Addr;
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, ControllerWsAction, RequestUpdateController};
use sqlx::{Pool, Sqlite};

use crate::app_state;
use crate::app_state::AppState;

#[get("/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("/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("/controllers/{controller_id}")]
pub async fn update(
	pool: web::Data<Pool<Sqlite>>,
	app_state: web::Data<Addr<AppState>>,
	path: web::Path<(String,)>,
	data: web::Json<RequestUpdateController>,
) -> 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)?;

	app_state
		.send(app_state::Action {
			controller_uid: uid.clone(),
			action: ControllerWsAction::Controller(return_controller.clone()),
		})
		.await??;

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

#[delete("/controllers/{controller_id}")]
pub async fn delete(
	pool: web::Data<Pool<Sqlite>>,
	app_state: web::Data<Addr<AppState>>,
	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())?;

	app_state
		.send(app_state::DisconnectController {
			controller_uid: uid.clone(),
		})
		.await??;

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