use arma_rs::{CallContext, Group};
use forge_models::locker::Item;
use forge_repositories::RedisLockerRepository;
use forge_services::LockerService;
use std::collections::HashMap;
use std::sync::LazyLock;

use crate::adapters::ExtensionRedisClient;
use crate::helpers::resolve_uid;
use crate::log::log;

static LOCKER_SERVICE: LazyLock<LockerService<RedisLockerRepository<ExtensionRedisClient>>> =
    LazyLock::new(|| {
        let redis_client = ExtensionRedisClient::new();
        let repository = RedisLockerRepository::new(redis_client);
        LockerService::new(repository)
    });

/// Creates the Arma 3 command group for locker operations.
///
/// Registers commands: `create`, `get`, `add`, `update`, `remove`, `delete`, `exists`.
pub fn group() -> Group {
    Group::new()
        .command("create", create_locker)
        .command("get", get_locker)
        .command("add", add_item)
        .command("update", update_locker)
        .command("patch", patch_item)
        .command("remove", remove_item)
        .command("delete", delete_locker)
        .command("exists", locker_exists)
}

/// Creates a new empty locker for a player.
///
/// Parameters: key
pub fn create_locker(call_context: CallContext, key: String) -> String {
    log(
        "locker",
        "DEBUG",
        &format!("Creating locker for key: {}", key),
    );

    let resolved_uid = match resolve_uid(&key, &call_context) {
        Some(uid) => {
            log("locker", "DEBUG", &format!("Resolved UID: {}", uid));
            uid
        }
        None => {
            let error_msg = format!("Error: Failed to resolve UID for key: {}", key);
            log("locker", "ERROR", &error_msg);
            return error_msg;
        }
    };

    match LOCKER_SERVICE.create_locker(resolved_uid.clone()) {
        Ok(locker) => {
            log(
                "locker",
                "INFO",
                &format!("Successfully created locker for: {}", resolved_uid),
            );
            match serde_json::to_string(&locker.items) {
                Ok(json) => json,
                Err(e) => {
                    let error_msg = format!("Error: Failed to serialize locker: {}", e);
                    log("locker", "ERROR", &error_msg);
                    error_msg
                }
            }
        }
        Err(e) => {
            let error_msg = format!("Error: {}", e);
            log(
                "locker",
                "ERROR",
                &format!("Failed to create locker '{}': {}", resolved_uid, e),
            );
            error_msg
        }
    }
}

/// Retrieves a player's locker by key/UID.
///
/// Returns JSON object with locker data including all items.
pub fn get_locker(call_context: CallContext, key: String) -> String {
    log(
        "locker",
        "DEBUG",
        &format!("Getting locker for key: {}", key),
    );

    let resolved_uid = match resolve_uid(&key, &call_context) {
        Some(uid) => {
            log("locker", "DEBUG", &format!("Resolved UID: {}", uid));
            uid
        }
        None => {
            let error_msg = format!("Error: Failed to resolve UID for key: {}", key);
            log("locker", "ERROR", &error_msg);
            return error_msg;
        }
    };

    match LOCKER_SERVICE.get_locker(resolved_uid.clone()) {
        Ok(locker) => {
            log(
                "locker",
                "INFO",
                &format!("Successfully got locker for: {}", resolved_uid),
            );
            match serde_json::to_string(&locker.items) {
                Ok(json) => {
                    log(
                        "locker",
                        "DEBUG",
                        &format!("Serialized locker to JSON: {}", json),
                    );
                    json
                }
                Err(e) => {
                    let error_msg = format!("Error: Failed to serialize locker: {}", e);
                    log("locker", "ERROR", &error_msg);
                    error_msg
                }
            }
        }
        Err(e) => {
            let error_msg = format!("Error: {}", e);
            log(
                "locker",
                "ERROR",
                &format!("Failed to get locker '{}': {}", resolved_uid, e),
            );
            error_msg
        }
    }
}

/// Adds a new item to a player's locker.
///
/// Parameters: key, json_data
pub fn add_item(call_context: CallContext, key: String, json_data: String) -> String {
    log(
        "locker",
        "DEBUG",
        &format!(
            "Adding item to locker for key: {} with data: {}",
            key, json_data
        ),
    );

    let resolved_uid = match resolve_uid(&key, &call_context) {
        Some(uid) => {
            log("locker", "DEBUG", &format!("Resolved UID: {}", uid));
            uid
        }
        None => {
            let error_msg = format!("Error: Failed to resolve UID for key: {}", key);
            log("locker", "ERROR", &error_msg);
            return error_msg;
        }
    };

    // Parse JSON data
    let data: serde_json::Value = match serde_json::from_str(&json_data) {
        Ok(d) => d,
        Err(e) => {
            let error_msg = format!("Error: Invalid JSON data: {}", e);
            log("locker", "ERROR", &error_msg);
            return error_msg;
        }
    };

    // Extract fields
    let category = match data.get("category").and_then(|v| v.as_str()) {
        Some(c) => c.to_string(),
        None => {
            let error_msg = "Error: Missing or invalid category".to_string();
            log("locker", "ERROR", &error_msg);
            return error_msg;
        }
    };

    let classname = match data.get("classname").and_then(|v| v.as_str()) {
        Some(c) => c.to_string(),
        None => {
            let error_msg = "Error: Missing or invalid classname".to_string();
            log("locker", "ERROR", &error_msg);
            return error_msg;
        }
    };

    let amount = match data.get("amount").and_then(|v| v.as_u64()) {
        Some(a) => a as u32,
        None => {
            let error_msg = "Error: Missing or invalid amount".to_string();
            log("locker", "ERROR", &error_msg);
            return error_msg;
        }
    };

    // Create item with validation
    let item = match Item::new(category, classname, amount) {
        Ok(i) => i,
        Err(e) => {
            let error_msg = format!("Error: Validation failed: {}", e);
            log("locker", "ERROR", &error_msg);
            return error_msg;
        }
    };

    match LOCKER_SERVICE.add_item(resolved_uid.clone(), item) {
        Ok(locker) => {
            log(
                "locker",
                "INFO",
                &format!("Successfully added item to locker for: {}", resolved_uid),
            );
            match serde_json::to_string(&locker.items) {
                Ok(json) => json,
                Err(e) => {
                    let error_msg = format!("Error: Failed to serialize locker: {}", e);
                    log("locker", "ERROR", &error_msg);
                    error_msg
                }
            }
        }
        Err(e) => {
            let error_msg = format!("Error: {}", e);
            log(
                "locker",
                "ERROR",
                &format!("Failed to add item to locker '{}': {}", resolved_uid, e),
            );
            error_msg
        }
    }
}

/// Updates the entire locker state (Bulk Sync).
///
/// Parameters: key, json_data (Map of items)
pub fn update_locker(call_context: CallContext, key: String, json_data: String) -> String {
    log(
        "locker",
        "DEBUG",
        &format!("Updating locker for key: {} with data: {}", key, json_data),
    );

    let resolved_uid = match resolve_uid(&key, &call_context) {
        Some(uid) => {
            log("locker", "DEBUG", &format!("Resolved UID: {}", uid));
            uid
        }
        None => {
            let error_msg = format!("Error: Failed to resolve UID for key: {}", key);
            log("locker", "ERROR", &error_msg);
            return error_msg;
        }
    };

    let items: HashMap<String, Item> = match serde_json::from_str(&json_data) {
        Ok(d) => d,
        Err(e) => {
            let error_msg = format!("Error: Invalid JSON data: {}", e);
            log("locker", "ERROR", &error_msg);
            return error_msg;
        }
    };

    match LOCKER_SERVICE.update_locker(resolved_uid.clone(), items) {
        Ok(locker) => {
            log(
                "locker",
                "INFO",
                &format!("Locker updated successfully for key: {}", resolved_uid),
            );
            match serde_json::to_string(&locker.items) {
                Ok(s) => s,
                Err(e) => format!("Error serializing locker: {}", e),
            }
        }
        Err(e) => {
            log("locker", "ERROR", &format!("Error updating locker: {}", e));
            format!("Error: {}", e)
        }
    }
}

/// Patches a specific item in the locker.
///
/// Parameters: key, json_data (Map with classname and optional amount)
pub fn patch_item(call_context: CallContext, key: String, json_data: String) -> String {
    log(
        "locker",
        "DEBUG",
        &format!("Patching item for key: {} with data: {}", key, json_data),
    );

    let resolved_uid = match resolve_uid(&key, &call_context) {
        Some(uid) => {
            log("locker", "DEBUG", &format!("Resolved UID: {}", uid));
            uid
        }
        None => {
            let error_msg = format!("Error: Failed to resolve UID for key: {}", key);
            log("locker", "ERROR", &error_msg);
            return error_msg;
        }
    };

    let data: serde_json::Value = match serde_json::from_str(&json_data) {
        Ok(d) => d,
        Err(e) => {
            let error_msg = format!("Error: Invalid JSON data: {}", e);
            log("locker", "ERROR", &error_msg);
            return error_msg;
        }
    };

    let classname = match data.get("classname").and_then(|v| v.as_str()) {
        Some(s) => s.to_string(),
        None => {
            let error_msg = "Error: Missing classname".to_string();
            log("locker", "ERROR", &error_msg);
            return error_msg;
        }
    };

    let amount = data
        .get("amount")
        .and_then(|v| v.as_u64())
        .map(|v| v as u32);

    match LOCKER_SERVICE.patch_item(resolved_uid.clone(), classname, amount) {
        Ok(locker) => {
            log(
                "locker",
                "INFO",
                &format!("Successfully patched item for: {}", resolved_uid),
            );
            match serde_json::to_string(&locker.items) {
                Ok(json) => json,
                Err(e) => {
                    let error_msg = format!("Error: Failed to serialize locker: {}", e);
                    log("locker", "ERROR", &error_msg);
                    error_msg
                }
            }
        }
        Err(e) => {
            let error_msg = format!("Error: {}", e);
            log(
                "locker",
                "ERROR",
                &format!("Failed to patch item '{}': {}", resolved_uid, e),
            );
            error_msg
        }
    }
}

/// Removes an item from the locker.
///
/// Parameters: key, classname
pub fn remove_item(call_context: CallContext, key: String, classname: String) -> String {
    log(
        "locker",
        "DEBUG",
        &format!("Removing item from locker for key: {}", key),
    );

    let resolved_uid = match resolve_uid(&key, &call_context) {
        Some(uid) => {
            log("locker", "DEBUG", &format!("Resolved UID: {}", uid));
            uid
        }
        None => {
            let error_msg = format!("Error: Failed to resolve UID for key: {}", key);
            log("locker", "ERROR", &error_msg);
            return error_msg;
        }
    };

    match LOCKER_SERVICE.remove_item(resolved_uid.clone(), classname) {
        Ok(locker) => {
            log(
                "locker",
                "INFO",
                &format!(
                    "Successfully removed item from locker for: {}",
                    resolved_uid
                ),
            );
            match serde_json::to_string(&locker.items) {
                Ok(json) => json,
                Err(e) => {
                    let error_msg = format!("Error: Failed to serialize locker: {}", e);
                    log("locker", "ERROR", &error_msg);
                    error_msg
                }
            }
        }
        Err(e) => {
            let error_msg = format!("Error: {}", e);
            log(
                "locker",
                "ERROR",
                &format!(
                    "Failed to remove item from locker '{}': {}",
                    resolved_uid, e
                ),
            );
            error_msg
        }
    }
}

/// Permanently deletes a player's locker.
pub fn delete_locker(call_context: CallContext, key: String) -> String {
    log(
        "locker",
        "DEBUG",
        &format!("Deleting locker for key: {}", key),
    );

    let resolved_uid = match resolve_uid(&key, &call_context) {
        Some(uid) => {
            log("locker", "DEBUG", &format!("Resolved UID: {}", uid));
            uid
        }
        None => {
            let error_msg = format!("Error: Failed to resolve UID for key: {}", key);
            log("locker", "ERROR", &error_msg);
            return error_msg;
        }
    };

    match LOCKER_SERVICE.delete_locker(resolved_uid.clone()) {
        Ok(()) => {
            log(
                "locker",
                "INFO",
                &format!("Successfully deleted locker for: {}", resolved_uid),
            );
            "OK".to_string()
        }
        Err(e) => {
            let error_msg = format!("Error: {}", e);
            log(
                "locker",
                "ERROR",
                &format!("Failed to delete locker '{}': {}", resolved_uid, e),
            );
            error_msg
        }
    }
}

/// Checks if a player has a locker (even if empty)
pub fn locker_exists(call_context: CallContext, key: String) -> String {
    log(
        "locker",
        "DEBUG",
        &format!("Checking if locker exists for key: {}", key),
    );

    let resolved_uid = match resolve_uid(&key, &call_context) {
        Some(uid) => {
            log("locker", "DEBUG", &format!("Resolved UID: {}", uid));
            uid
        }
        None => {
            let error_msg = format!("Error: Failed to resolve UID for key: {}", key);
            log("locker", "ERROR", &error_msg);
            return error_msg;
        }
    };

    match LOCKER_SERVICE.locker_exists(resolved_uid.clone()) {
        Ok(exists) => {
            log(
                "locker",
                "INFO",
                &format!("Locker exists for: {}", resolved_uid),
            );
            exists.to_string()
        }
        Err(e) => {
            let error_msg = format!("Error: {}", e);
            log(
                "locker",
                "ERROR",
                &format!(
                    "Failed to check if locker exists for '{}': {}",
                    resolved_uid, e
                ),
            );
            error_msg
        }
    }
}
