use serde::Deserialize;
use tauri::State;

use crate::db::connection::DbManager;
use crate::domains::series_world::service;
use crate::error::AppError;
use crate::shared::session::SessionState;
use crate::shared::types::Lang;

fn get_session(session: &State<SessionState>) -> Result<(String, Lang), AppError> {
    let session_guard = session.lock().map_err(|e| AppError::Internal(format!("Session lock failed: {}", e)))?;
    let user_id = session_guard.get_user_id().map_err(|e| AppError::Auth(e))?.to_string();
    let lang = session_guard.lang;
    Ok((user_id, lang))
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetSeriesWorldListData { pub series_id: String }

#[tauri::command]
pub fn get_series_world_list(data: GetSeriesWorldListData, db: State<DbManager>, session: State<SessionState>) -> Result<Vec<service::SeriesWorldListProps>, AppError> {
    let (user_id, lang) = get_session(&session)?;
    let db_manager = db.lock().map_err(|e| AppError::Internal(format!("DB lock failed: {}", e)))?;
    let conn = db_manager.get_connection(&user_id)?;
    service::get_world_list(conn, &user_id, &data.series_id, lang)
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AddSeriesWorldData { pub series_id: String, pub name: String }

#[tauri::command]
pub fn add_series_world(data: AddSeriesWorldData, db: State<DbManager>, session: State<SessionState>) -> Result<String, AppError> {
    let (user_id, lang) = get_session(&session)?;
    let db_manager = db.lock().map_err(|e| AppError::Internal(format!("DB lock failed: {}", e)))?;
    let conn = db_manager.get_connection(&user_id)?;
    service::add_world(conn, &user_id, &data.series_id, &data.name, lang)
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateSeriesWorldData {
    pub world_id: String,
    pub world: service::SeriesWorldUpdateProps,
}

#[tauri::command]
pub fn update_series_world(data: UpdateSeriesWorldData, db: State<DbManager>, session: State<SessionState>) -> Result<bool, AppError> {
    let (user_id, lang) = get_session(&session)?;
    let db_manager = db.lock().map_err(|e| AppError::Internal(format!("DB lock failed: {}", e)))?;
    let conn = db_manager.get_connection(&user_id)?;
    service::update_world(conn, &user_id, &data.world_id, data.world, lang)
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AddSeriesWorldElementData {
    pub world_id: String,
    pub element_type: i64,
    pub name: String,
    pub description: Option<String>,
}

#[tauri::command]
pub fn add_series_world_element(data: AddSeriesWorldElementData, db: State<DbManager>, session: State<SessionState>) -> Result<String, AppError> {
    let (user_id, lang) = get_session(&session)?;
    let db_manager = db.lock().map_err(|e| AppError::Internal(format!("DB lock failed: {}", e)))?;
    let conn = db_manager.get_connection(&user_id)?;
    service::add_element(conn, &user_id, &data.world_id, data.element_type, &data.name, lang, data.description.as_deref())
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DeleteSeriesWorldElementData { pub element_id: String, pub deleted_at: i64 }

#[tauri::command]
pub fn delete_series_world_element(data: DeleteSeriesWorldElementData, db: State<DbManager>, session: State<SessionState>) -> Result<bool, AppError> {
    let (user_id, lang) = get_session(&session)?;
    let db_manager = db.lock().map_err(|e| AppError::Internal(format!("DB lock failed: {}", e)))?;
    let conn = db_manager.get_connection(&user_id)?;
    service::delete_element(conn, &user_id, &data.element_id, data.deleted_at, lang)
}
