// src/modes/common/command_mode.rs

use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
use crate::config::binds::config::Config;
use crate::services::grpc_client::GrpcClient;
use crate::state::pages::form::FormState;
use crate::state::{app::state::AppState, pages::auth::LoginState, pages::auth::RegisterState};
use crate::modes::common::commands::CommandHandler;
use crate::tui::terminal::core::TerminalCore;
use crate::tui::functions::common::form::{save, revert};
use crate::modes::handlers::event::EventOutcome;
use crate::tui::functions::common::form::SaveOutcome;
use anyhow::Result;

pub async fn handle_command_event(
    key: KeyEvent,
    config: &Config,
    app_state: &mut AppState,
    login_state: &LoginState,
    register_state: &RegisterState,
    form_state: &mut FormState,
    command_input: &mut String,
    command_message: &mut String,
    grpc_client: &mut GrpcClient,
    command_handler: &mut CommandHandler,
    terminal: &mut TerminalCore,
    current_position: &mut u64,
    total_count: u64,
) -> Result<EventOutcome> {
    // Exit command mode (via configurable keybinding)
    if config.is_exit_command_mode(key.code, key.modifiers) {
        command_input.clear();
        *command_message = "".to_string();
        return Ok(EventOutcome::Ok("Exited command mode".to_string()));
    }

    // Execute command (via configurable keybinding, defaults to Enter)
    if config.is_command_execute(key.code, key.modifiers) {
        return process_command(
            config,
            form_state,
            app_state,
            login_state,
            register_state,
            command_input,
            command_message,
            grpc_client,
            command_handler,
            terminal,
            current_position,
            total_count,
        ).await;
    }

    // Backspace (via configurable keybinding, defaults to Backspace)
    if config.is_command_backspace(key.code, key.modifiers) {
        command_input.pop();
        return Ok(EventOutcome::Ok("".to_string()));
    }

    // Regular character input - accept any character in command mode
    if let KeyCode::Char(c) = key.code {
        // Accept regular or shifted characters (e.g., 'a' or 'A')
        if key.modifiers.is_empty() || key.modifiers == KeyModifiers::SHIFT {
            command_input.push(c);
            return Ok(EventOutcome::Ok("".to_string()));
        }
    }

    // Ignore all other keys
    Ok(EventOutcome::Ok("".to_string()))
}

async fn process_command(
    config: &Config,
    form_state: &mut FormState,
    app_state: &mut AppState,
    login_state: &LoginState,
    register_state: &RegisterState,
    command_input: &mut String,
    command_message: &mut String,
    grpc_client: &mut GrpcClient,
    command_handler: &mut CommandHandler,
    terminal: &mut TerminalCore,
    current_position: &mut u64,
    total_count: u64,
) -> Result<EventOutcome> {
    // Clone the trimmed command to avoid borrow issues
    let command = command_input.trim().to_string();
    if command.is_empty() {
        *command_message = "Empty command".to_string();
        return Ok(EventOutcome::Ok(command_message.clone()));
    }

    // Get the action for the command (now checks global and common bindings too)
    let action = config.get_action_for_command(&command)
        .unwrap_or("unknown");

    match action {
        "force_quit" | "save_and_quit" | "quit" => {
            let (should_exit, message) = command_handler
                .handle_command(
                    action,
                    terminal,
                    app_state,
                    form_state,
                    login_state,
                    register_state,
                )
                .await?;
            command_input.clear();
            if should_exit {
                Ok(EventOutcome::Exit(message))
            } else {
                Ok(EventOutcome::Ok(message))
            }
        },
        "save" => {
            let outcome = save(
                app_state,
                form_state,
                grpc_client,
            ).await?;
            let message = match outcome {
                SaveOutcome::CreatedNew(_) => "New entry created".to_string(),
                SaveOutcome::UpdatedExisting => "Entry updated".to_string(),
                SaveOutcome::NoChange => "No changes to save".to_string(),
            };
            command_input.clear();
            Ok(EventOutcome::DataSaved(outcome, message))
        },
        "revert" => {
            let message = revert(
                form_state,
                grpc_client,
            ).await?;
            command_input.clear();
            Ok(EventOutcome::Ok(message))
        },
        _ => {
            let message = format!("Unhandled action: {}", action);
            command_input.clear();
            Ok(EventOutcome::Ok(message))
        }
    }
}
