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

use crate::db::connection::DbManager;
use crate::domains::series_character::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 GetSeriesCharacterListData { pub series_id: String }

#[tauri::command]
pub fn get_series_character_list(data: GetSeriesCharacterListData, db: State<DbManager>, session: State<SessionState>) -> Result<Vec<service::SeriesCharacterListProps>, 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_character_list(conn, &user_id, &data.series_id, lang)
}

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

#[tauri::command]
pub fn get_series_character_attributes(data: GetSeriesCharacterAttributesData, db: State<DbManager>, session: State<SessionState>) -> Result<service::CharacterAttributesResponse, 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_character_attributes(conn, &user_id, &data.character_id, lang)
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AddSeriesCharacterData {
    pub series_id: String,
    pub character: service::SeriesCharacterPropsPost,
}

#[tauri::command]
pub fn add_series_character(data: AddSeriesCharacterData, 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_new_character(conn, &user_id, &data.character, &data.series_id, lang)
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateSeriesCharacterData {
    pub character: service::SeriesCharacterPropsPost,
}

#[tauri::command]
pub fn update_series_character(data: UpdateSeriesCharacterData, 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_character(conn, &user_id, &data.character, lang)
}

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

#[tauri::command]
pub fn delete_series_character(data: DeleteSeriesCharacterData, 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_character(conn, &user_id, &data.character_id, data.deleted_at, lang)
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AddSeriesCharacterAttributeData {
    pub character_id: String,
    pub r#type: String,
    pub name: String,
}

#[tauri::command]
pub fn add_series_character_attribute(data: AddSeriesCharacterAttributeData, 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_new_attribute(conn, &data.character_id, &user_id, &data.r#type, &data.name, lang)
}

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

#[tauri::command]
pub fn delete_series_character_attribute(data: DeleteSeriesCharacterAttributeData, 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_attribute(conn, &user_id, &data.attribute_id, data.deleted_at, lang)
}
