// examples/suggestions2.rs
//! Production-ready Tab-triggered suggestions demonstration
//!
//! This example demonstrates:
//! - Tab-triggered suggestions dropdown
//! - Non-blocking architecture for real network/database calls
//! - Multiple suggestion field types
//! - Professional-grade user experience
//!
//! Run with:
//! cargo run --example suggestions2 --features "gui,cursor-style,suggestions"

// REQUIRE cursor-style feature - example won't compile without it
#[cfg(not(feature = "cursor-style"))]
compile_error!(
    "This example requires the 'cursor-style' feature. \
     Run with: cargo run --example suggestions2 --features \"gui,cursor-style,suggestions\""
);

use crossterm::{
    event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyModifiers},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{
    backend::{Backend, CrosstermBackend},
    layout::{Constraint, Direction, Layout},
    style::{Color, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph},
    Frame, Terminal,
};
use std::io;

use tui_canvas::{
    render_canvas_default,
    AppMode,
    CursorManager, // This import only exists when cursor-style feature is enabled
    suggestions::render::render_suggestions_dropdown,
    DataProvider, FormEditor, SuggestionItem,
};

// FormEditor wrapper for suggestions demo
struct AutoCursorFormEditor<D: DataProvider> {
    editor: FormEditor<D>,
    has_unsaved_changes: bool,
    debug_message: String,
    command_buffer: String, // For multi-key vim commands like "gg"
}

impl<D: DataProvider> AutoCursorFormEditor<D> {
    fn new(data_provider: D) -> Self {
        Self {
            editor: FormEditor::new(data_provider),
            has_unsaved_changes: false,
            debug_message: "🚀 Production-Ready Tab-Triggered Suggestions Demo - Copy this architecture for your app!".to_string(),
            command_buffer: String::new(),
        }
    }

    fn dismiss_suggestions(&mut self) {
        self.editor.dismiss_suggestions();
    }

    fn trigger_suggestions(&mut self) -> Option<(usize, String)> {
        self.editor.trigger_suggestions()
    }

    fn apply_suggestions(&mut self, items: Vec<SuggestionItem>) {
        self.editor.apply_suggestions(items);
    }

    // Command buffer handling

    fn clear_command_buffer(&mut self) {
        self.command_buffer.clear();
    }

    fn add_to_command_buffer(&mut self, ch: char) {
        self.command_buffer.push(ch);
    }

    fn get_command_buffer(&self) -> &str {
        &self.command_buffer
    }

    fn has_pending_command(&self) -> bool {
        !self.command_buffer.is_empty()
    }

    // Visual highlight mode support

    fn enter_visual_mode(&mut self) {
        self.editor.enter_highlight_mode();
        self.debug_message = "🔥 VISUAL MODE - Cursor: Blinking Block █".to_string();
    }

    fn enter_visual_line_mode(&mut self) {
        self.editor.enter_highlight_line_mode();
        self.debug_message = "🔥 VISUAL LINE MODE - Cursor: Blinking Block █".to_string();
    }

    fn exit_visual_mode(&mut self) {
        self.editor.exit_highlight_mode();
        self.debug_message = "🔒 NORMAL MODE - Cursor: Steady Block █".to_string();
    }

    fn update_visual_selection(&mut self) {
        if self.editor.is_highlight_mode() {
            use tui_canvas::canvas::state::SelectionState;
            match self.editor.selection_state() {
                SelectionState::Characterwise { anchor } => {
                    self.debug_message = format!(
                        "🎯 Visual selection: anchor=({},{}) current=({},{}) - Cursor: Blinking Block █",
                        anchor.0, anchor.1,
                        self.editor.current_field(),
                        self.editor.cursor_position()
                    );
                }
                SelectionState::Linewise { anchor_field } => {
                    self.debug_message = format!(
                        "🎯 Visual LINE selection: anchor={} current={} - Cursor: Blinking Block █",
                        anchor_field,
                        self.editor.current_field()
                    );
                }
                _ => {}
            }
        }
    }

    // Movement with visual updates

    fn move_left(&mut self) {
        let _ = self.editor.move_left();
        self.update_visual_selection();
    }

    fn move_right(&mut self) {
        let _ = self.editor.move_right();
        self.update_visual_selection();
    }

    fn move_up(&mut self) {
        let _ = self.editor.move_up();
        self.update_visual_selection();
    }

    fn move_down(&mut self) {
        let _ = self.editor.move_down();
        self.update_visual_selection();
    }

    fn move_word_next(&mut self) {
        self.editor.move_word_next();
        self.update_visual_selection();
    }

    fn move_word_prev(&mut self) {
        self.editor.move_word_prev();
        self.update_visual_selection();
    }

    fn move_word_end(&mut self) {
        self.editor.move_word_end();
        self.update_visual_selection();
    }

    fn move_word_end_prev(&mut self) {
        self.editor.move_word_end_prev();
        self.update_visual_selection();
    }

    fn move_line_start(&mut self) {
        self.editor.move_line_start();
        self.update_visual_selection();
    }

    fn move_line_end(&mut self) {
        self.editor.move_line_end();
        self.update_visual_selection();
    }

    fn move_first_line(&mut self) {
        let _ = self.editor.move_first_line();
        self.update_visual_selection();
    }

    fn move_last_line(&mut self) {
        let _ = self.editor.move_last_line();
        self.update_visual_selection();
    }

    fn prev_field(&mut self) {
        let _ = self.editor.prev_field();
        self.update_visual_selection();
    }

    fn next_field(&mut self) {
        let _ = self.editor.next_field();
        self.update_visual_selection();
    }

    // Delete operations

    fn delete_backward(&mut self) -> anyhow::Result<()> {
        let result = self.editor.delete_backward();
        if result.is_ok() {
            self.has_unsaved_changes = true;
            self.debug_message = "⌫ Deleted character backward".to_string();
        }
        result
    }

    fn delete_forward(&mut self) -> anyhow::Result<()> {
        let result = self.editor.delete_forward();
        if result.is_ok() {
            self.has_unsaved_changes = true;
            self.debug_message = "⌦ Deleted character forward".to_string();
        }
        result
    }

    // Suggestions control wrappers

    fn open_suggestions(&mut self, field_index: usize) {
        self.editor.open_suggestions(field_index);
    }

    // Mode transitions with automatic cursor management

    fn enter_edit_mode(&mut self) {
        self.editor.enter_edit_mode(); // 🎯 Library automatically sets cursor to bar |
        self.debug_message =
            "✏️  INSERT MODE - Cursor: Steady Bar | - Press Tab for suggestions".to_string();
    }

    fn enter_append_mode(&mut self) {
        self.editor.enter_append_mode(); // 🎯 Library automatically positions cursor and sets mode
        self.debug_message =
            "✏️  INSERT (append) - Cursor: Steady Bar | - Press Tab for suggestions".to_string();
    }

    fn exit_edit_mode(&mut self) {
        let _ = self.editor.exit_edit_mode(); // 🎯 Library automatically sets cursor to block █
        self.exit_visual_mode();
        self.debug_message = "🔒 NORMAL MODE - Cursor: Steady Block █".to_string();
    }

    fn insert_char(&mut self, ch: char) -> anyhow::Result<()> {
        let result = self.editor.insert_char(ch);
        if result.is_ok() {
            self.has_unsaved_changes = true;
        }
        result
    }

    // Suggestions

    /// Trigger suggestions and fetch results synchronously.
    fn trigger_and_fetch_suggestions(&mut self, provider: &mut ProductionSuggestionsProvider) {
        if let Some((field_index, query)) = self.editor.trigger_suggestions() {
            let results = provider.fetch_suggestions(field_index, &query);
            self.editor.apply_suggestions(results);
            self.editor.update_inline_completion();
            if self.editor.suggestions().is_empty() {
                self.set_debug_message(format!("🔍 No matches for '{query}'"));
            } else {
                self.set_debug_message(format!(
                    "✨ {} matches for '{}'",
                    self.editor.suggestions().len(),
                    query
                ));
            }
        }
    }

    fn suggestions_next(&mut self) {
        self.editor.suggestions_next();
    }

    fn apply_suggestion(&mut self) -> Option<String> {
        self.editor.apply_suggestion()
    }

    fn is_suggestions_active(&self) -> bool {
        self.editor.is_suggestions_active()
    }

    fn suggestions(&self) -> &[SuggestionItem] {
        self.editor.suggestions()
    }

    pub fn update_inline_completion(&mut self) {
        self.editor.update_inline_completion();
    }

    // Manual cursor override demonstration

    fn demo_manual_cursor_control(&mut self) -> std::io::Result<()> {
        CursorManager::update_for_mode(AppMode::Command)?;
        self.debug_message = "🔧 Manual override: Command cursor _".to_string();
        Ok(())
    }

    fn restore_automatic_cursor(&mut self) -> std::io::Result<()> {
        CursorManager::update_for_mode(self.editor.mode())?;
        self.debug_message = "🎯 Restored automatic cursor management".to_string();
        Ok(())
    }

    // Delegate to original editor

    fn current_field(&self) -> usize {
        self.editor.current_field()
    }

    fn cursor_position(&self) -> usize {
        self.editor.cursor_position()
    }

    fn mode(&self) -> AppMode {
        self.editor.mode()
    }

    fn current_text(&self) -> &str {
        let field_index = self.editor.current_field();
        self.editor.data_provider().field_value(field_index)
    }

    fn data_provider(&self) -> &D {
        self.editor.data_provider()
    }

    fn ui_state(&self) -> &tui_canvas::EditorState {
        self.editor.ui_state()
    }

    fn set_mode(&mut self, mode: AppMode) {
        self.editor.set_mode(mode); // 🎯 Library automatically updates cursor
        if mode != AppMode::Sel {
            self.exit_visual_mode();
        }
    }

    // Status and debug

    fn set_debug_message(&mut self, msg: String) {
        self.debug_message = msg;
    }

    fn debug_message(&self) -> &str {
        &self.debug_message
    }

    fn has_unsaved_changes(&self) -> bool {
        self.has_unsaved_changes
    }
}

// Production data model

struct ApplicationData {
    fields: Vec<(String, String)>,
}

impl ApplicationData {
    fn new() -> Self {
        Self {
            fields: vec![
                ("🍎 Favorite Fruit".to_string(), "".to_string()),
                ("💼 Job Role".to_string(), "".to_string()),
                ("💻 Programming Language".to_string(), "".to_string()),
                ("🌍 Country".to_string(), "".to_string()),
                ("🎨 Favorite Color".to_string(), "".to_string()),
            ],
        }
    }
}

impl DataProvider for ApplicationData {
    fn field_count(&self) -> usize {
        self.fields.len()
    }

    fn field_name(&self, index: usize) -> &str {
        &self.fields[index].0
    }

    fn field_value(&self, index: usize) -> &str {
        &self.fields[index].1
    }

    fn set_field_value(&mut self, index: usize, value: String) {
        self.fields[index].1 = value;
    }

    fn supports_suggestions(&self, field_index: usize) -> bool {
        // Configure which fields support suggestions
        field_index < 5
    }

    fn display_value(&self, _index: usize) -> Option<&str> {
        None
    }
}

// Production suggestions provider (sync)

/// Suggestions provider (sync)
///
/// Replace the data sources below with your implementation:
/// - REST API calls (reqwest, ureq)
/// - Database queries (sqlx, diesel)
/// - Search engines (elasticsearch, algolia)
/// - Cache lookups (redis, memcached)
/// - GraphQL queries
/// - gRPC services
///
/// For async data sources: fetch externally and call editor.apply_suggestions() later
struct ProductionSuggestionsProvider {}

impl ProductionSuggestionsProvider {
    fn new() -> Self {
        Self {}
    }

    /// Get fruit suggestions (replace with your API call)
    fn get_fruit_suggestions(&self, query: &str) -> Vec<SuggestionItem> {
        let fruits = vec![
            ("Apple", "🍎 Crisp and sweet"),
            ("Banana", "🍌 Rich in potassium"),
            ("Cherry", "🍒 Small and tart"),
            ("Date", "📅 Sweet and chewy"),
            ("Elderberry", "🫐 Dark purple berry"),
            ("Fig", "🍇 Sweet Mediterranean fruit"),
            ("Grape", "🍇 Perfect for wine"),
            ("Honeydew", "🍈 Sweet melon"),
            ("Avocado", "🥑 Creamy and nutritious"),
        ];

        self.filter_suggestions(fruits, query)
    }

    /// Get job suggestions (replace with your database query)
    fn get_job_suggestions(&self, query: &str) -> Vec<SuggestionItem> {
        let jobs = vec![
            ("Software Engineer", "👨‍💻 Build applications"),
            ("Product Manager", "📋 Manage product roadmap"),
            ("Data Scientist", "📊 Analyze data patterns"),
            ("UX Designer", "🎨 Design user experiences"),
            ("DevOps Engineer", "⚙️ Manage infrastructure"),
            ("Marketing Manager", "📢 Drive growth"),
            ("Sales Representative", "💰 Generate revenue"),
            ("Accountant", "💼 Manage finances"),
        ];

        self.filter_suggestions(jobs, query)
    }

    /// Get language suggestions (replace with your cache lookup)
    fn get_language_suggestions(&self, query: &str) -> Vec<SuggestionItem> {
        let languages = vec![
            ("Rust", "🦀 Systems programming"),
            ("Python", "🐍 Versatile and popular"),
            ("JavaScript", "⚡ Web development"),
            ("TypeScript", "🔷 Typed JavaScript"),
            ("Go", "🏃 Fast and simple"),
            ("Java", "☕ Enterprise favorite"),
            ("C++", "⚡ High performance"),
            ("Swift", "🍎 iOS development"),
        ];

        self.filter_suggestions(languages, query)
    }

    /// Get country suggestions (replace with your geographic API)
    fn get_country_suggestions(&self, query: &str) -> Vec<SuggestionItem> {
        let countries = vec![
            ("United States", "🇺🇸 North America"),
            ("Canada", "🇨🇦 Great neighbors"),
            ("United Kingdom", "🇬🇧 Tea and crumpets"),
            ("Germany", "🇩🇪 Engineering excellence"),
            ("France", "🇫🇷 Art and cuisine"),
            ("Japan", "🇯🇵 Technology hub"),
            ("Australia", "🇦🇺 Down under"),
            ("Brazil", "🇧🇷 Carnival country"),
        ];

        self.filter_suggestions(countries, query)
    }

    /// Get color suggestions (local data)
    fn get_color_suggestions(&self, query: &str) -> Vec<SuggestionItem> {
        let colors = vec![
            ("Red", "🔴 Bold and energetic"),
            ("Blue", "🔵 Calm and trustworthy"),
            ("Green", "🟢 Natural and fresh"),
            ("Yellow", "🟡 Bright and cheerful"),
            ("Purple", "🟣 Royal and mysterious"),
            ("Orange", "🟠 Warm and vibrant"),
            ("Pink", "🩷 Soft and gentle"),
            ("Black", "⚫ Classic and elegant"),
        ];

        self.filter_suggestions(colors, query)
    }

    /// Main suggestions entry point - route to appropriate data source
    fn fetch_suggestions(&mut self, field_index: usize, query: &str) -> Vec<SuggestionItem> {
        match field_index {
            0 => self.get_fruit_suggestions(query),
            1 => self.get_job_suggestions(query),
            2 => self.get_language_suggestions(query),
            3 => self.get_country_suggestions(query),
            4 => self.get_color_suggestions(query),
            _ => Vec::new(),
        }
    }

    /// Generic filtering helper (reusable for any data source)
    fn filter_suggestions(&self, items: Vec<(&str, &str)>, query: &str) -> Vec<SuggestionItem> {
        let query_lower = query.to_lowercase();

        items
            .iter()
            .filter(|(item, _)| query.is_empty() || item.to_lowercase().starts_with(&query_lower))
            .map(|(item, description)| {
                SuggestionItem::new(format!("{item} - {description}"), *item)
            })
            .collect()
    }
}

/// Production-ready key handling with Tab-triggered suggestions
fn handle_key_press(
    key: KeyCode,
    modifiers: KeyModifiers,
    editor: &mut AutoCursorFormEditor<ApplicationData>,
    suggestions_provider: &mut ProductionSuggestionsProvider,
) -> anyhow::Result<bool> {
    let mode = editor.mode();

    // Quit handling
    if (key == KeyCode::Char('q') && modifiers.contains(KeyModifiers::CONTROL))
        || (key == KeyCode::Char('c') && modifiers.contains(KeyModifiers::CONTROL))
        || key == KeyCode::F(10)
    {
        return Ok(false);
    }

    match (mode, key, modifiers) {
        // Tab triggered suggestions handling
        (_, KeyCode::Tab, _) => {
            if editor.is_suggestions_active() {
                // Cycle through suggestions
                editor.suggestions_next();
                editor.set_debug_message("📍 Next suggestion".to_string());
            } else if editor
                .data_provider()
                .supports_suggestions(editor.current_field())
            {
                // Trigger suggestions
                editor.trigger_and_fetch_suggestions(suggestions_provider);
            } else {
                editor.next_field();
                editor.set_debug_message("Tab: next field".to_string());
            }
        }

        // Enter: Apply suggestion or move to next field
        (_, KeyCode::Enter, _) => {
            if editor.is_suggestions_active() {
                if let Some(applied) = editor.apply_suggestion() {
                    editor.set_debug_message(format!("✅ Selected: {applied}"));
                } else {
                    editor.set_debug_message("❌ No suggestion selected".to_string());
                }
            } else {
                editor.next_field();
                let field_names = ["Fruit", "Job", "Language", "Country", "Color"];
                let field_name = field_names.get(editor.current_field()).unwrap_or(&"Field");
                editor.set_debug_message(format!("Enter: moved to {field_name} field"));
            }
        }

        // Escape: Dismiss suggestions or exit mode
        (_, KeyCode::Esc, _) => {
            if editor.is_suggestions_active() {
                editor.dismiss_suggestions();
                editor.set_debug_message("❌ Suggestions closed".to_string());
            } else {
                match mode {
                    AppMode::Ins => {
                        editor.exit_edit_mode();
                    }
                    AppMode::Sel => {
                        editor.exit_visual_mode();
                    }
                    _ => {
                        editor.clear_command_buffer();
                    }
                }
            }
        }

        // Mode transitions no autosuggestions
        (AppMode::Nor, KeyCode::Char('i'), _) => {
            editor.enter_edit_mode();
            editor.clear_command_buffer();
            // For auto-suggestions on insert: add `editor.auto_trigger_suggestions(suggestions_provider).await;`
        }
        (AppMode::Nor, KeyCode::Char('a'), _) => {
            editor.enter_append_mode();
            editor.set_debug_message(
                "✏️  INSERT (append) - Cursor: Steady Bar | - Press Tab for suggestions"
                    .to_string(),
            );
            editor.clear_command_buffer();
        }
        (AppMode::Nor, KeyCode::Char('A'), _) => {
            editor.move_line_end();
            editor.enter_edit_mode();
            editor.set_debug_message(
                "✏️  INSERT (end of line) - Cursor: Steady Bar | - Press Tab for suggestions"
                    .to_string(),
            );
            editor.clear_command_buffer();
        }

        // From Normal Mode: Enter visual modes
        (AppMode::Nor, KeyCode::Char('v'), _) => {
            editor.enter_visual_mode();
            editor.clear_command_buffer();
        }
        (AppMode::Nor, KeyCode::Char('V'), _) => {
            editor.enter_visual_line_mode();
            editor.clear_command_buffer();
        }

        // Cursor management demonstration
        (AppMode::Nor, KeyCode::F(1), _) => {
            editor.demo_manual_cursor_control()?;
        }
        (AppMode::Nor, KeyCode::F(2), _) => {
            editor.restore_automatic_cursor()?;
        }

        // Movement vim style navigation

        // Basic movement (hjkl and arrows)
        (AppMode::Nor | AppMode::Sel, KeyCode::Char('h'), _)
        | (AppMode::Nor | AppMode::Sel, KeyCode::Left, _) => {
            editor.move_left();
            editor.set_debug_message("← left".to_string());
            editor.clear_command_buffer();
        }
        (AppMode::Nor | AppMode::Sel, KeyCode::Char('l'), _)
        | (AppMode::Nor | AppMode::Sel, KeyCode::Right, _) => {
            editor.move_right();
            editor.set_debug_message("→ right".to_string());
            editor.clear_command_buffer();
        }
        (AppMode::Nor | AppMode::Sel, KeyCode::Char('j'), _)
        | (AppMode::Nor | AppMode::Sel, KeyCode::Down, _) => {
            editor.move_down();
            let field_names = ["Fruit", "Job", "Language", "Country", "Color"];
            let field_name = field_names.get(editor.current_field()).unwrap_or(&"Field");
            editor.set_debug_message(format!("↓ moved to {field_name} field"));
            editor.clear_command_buffer();
        }
        (AppMode::Nor | AppMode::Sel, KeyCode::Char('k'), _)
        | (AppMode::Nor | AppMode::Sel, KeyCode::Up, _) => {
            editor.move_up();
            let field_names = ["Fruit", "Job", "Language", "Country", "Color"];
            let field_name = field_names.get(editor.current_field()).unwrap_or(&"Field");
            editor.set_debug_message(format!("↑ moved to {field_name} field"));
            editor.clear_command_buffer();
        }

        // Word movement
        (AppMode::Nor | AppMode::Sel, KeyCode::Char('w'), _) => {
            editor.move_word_next();
            editor.set_debug_message("w: next word start".to_string());
            editor.clear_command_buffer();
        }
        (AppMode::Nor | AppMode::Sel, KeyCode::Char('b'), _) => {
            editor.move_word_prev();
            editor.set_debug_message("b: previous word start".to_string());
            editor.clear_command_buffer();
        }
        (AppMode::Nor | AppMode::Sel, KeyCode::Char('e'), _) => {
            editor.move_word_end();
            editor.set_debug_message("e: word end".to_string());
            editor.clear_command_buffer();
        }

        // Line movement
        (AppMode::Nor | AppMode::Sel, KeyCode::Char('0'), _)
        | (AppMode::Nor | AppMode::Sel, KeyCode::Home, _) => {
            editor.move_line_start();
            editor.set_debug_message("0: line start".to_string());
        }
        (AppMode::Nor | AppMode::Sel, KeyCode::Char('$'), _)
        | (AppMode::Nor | AppMode::Sel, KeyCode::End, _) => {
            editor.move_line_end();
            editor.set_debug_message("$: line end".to_string());
        }

        // Document movement
        (AppMode::Nor | AppMode::Sel, KeyCode::Char('g'), _) => {
            if editor.get_command_buffer() == "g" {
                editor.move_first_line();
                editor.set_debug_message("gg: first field (Fruit)".to_string());
                editor.clear_command_buffer();
            } else {
                editor.clear_command_buffer();
                editor.add_to_command_buffer('g');
                editor.set_debug_message("g".to_string());
            }
        }
        (AppMode::Nor | AppMode::Sel, KeyCode::Char('G'), _) => {
            editor.move_last_line();
            editor.set_debug_message("G: last field (Color)".to_string());
            editor.clear_command_buffer();
        }

        // Edit mode movement
        (AppMode::Ins, KeyCode::Left, m) if m.contains(KeyModifiers::CONTROL) => {
            editor.move_word_prev();
            editor.set_debug_message("Ctrl+← word back".to_string());
        }
        (AppMode::Ins, KeyCode::Right, m) if m.contains(KeyModifiers::CONTROL) => {
            editor.move_word_next();
            editor.set_debug_message("Ctrl+→ word forward".to_string());
        }
        (AppMode::Ins, KeyCode::Left, _) => {
            editor.move_left();
        }
        (AppMode::Ins, KeyCode::Right, _) => {
            editor.move_right();
        }
        (AppMode::Ins, KeyCode::Up, _) => {
            editor.move_up();
        }
        (AppMode::Ins, KeyCode::Down, _) => {
            editor.move_down();
        }
        (AppMode::Ins, KeyCode::Home, _) => {
            editor.move_line_start();
        }
        (AppMode::Ins, KeyCode::End, _) => {
            editor.move_line_end();
        }

        // Delete operations autofetch when suggestions active
        (AppMode::Ins, KeyCode::Backspace, _) => {
            editor.delete_backward()?;
            if editor.is_suggestions_active() {
                editor.trigger_and_fetch_suggestions(suggestions_provider);
            }
        }
        (AppMode::Ins, KeyCode::Delete, _) => {
            editor.delete_forward()?;
            if editor.is_suggestions_active() {
                editor.trigger_and_fetch_suggestions(suggestions_provider);
            }
        }

        // Delete operations in normal mode (vim x)
        (AppMode::Nor, KeyCode::Char('x'), _) => {
            editor.delete_forward()?;
            editor.set_debug_message("x: deleted character".to_string());
        }
        (AppMode::Nor, KeyCode::Char('X'), _) => {
            editor.delete_backward()?;
            editor.set_debug_message("X: deleted character backward".to_string());
        }

        // Character input autofetch when suggestions active
        (AppMode::Ins, KeyCode::Char(c), m) if !m.contains(KeyModifiers::CONTROL) => {
            editor.insert_char(c)?;
            if editor.is_suggestions_active() {
                editor.trigger_and_fetch_suggestions(suggestions_provider);
            }
        }

        // Debug info commands
        (AppMode::Nor, KeyCode::Char('?'), _) => {
            let field_names = ["Fruit🍎", "Job💼", "Language💻", "Country🌍", "Color🎨"];
            let current_field_name = field_names
                .get(editor.current_field())
                .unwrap_or(&"Unknown");
            editor.set_debug_message(format!(
                "Field: {} ({}/{}), Pos: {}, Mode: {:?}",
                current_field_name,
                editor.current_field() + 1,
                editor.data_provider().field_count(),
                editor.cursor_position(),
                editor.mode()
            ));
        }

        _ => {
            if editor.has_pending_command() {
                editor.clear_command_buffer();
                editor.set_debug_message("Invalid command sequence".to_string());
            } else {
                let field_names = ["Fruit", "Job", "Language", "Country", "Color"];
                let current_field = field_names.get(editor.current_field()).unwrap_or(&"Field");
                editor.set_debug_message(format!(
                    "{current_field} field - Try: i=insert, Tab=suggestions, j/k=move. Key: {key:?}"
                ));
            }
        }
    }

    Ok(true)
}

fn run_app<B: Backend>(
    terminal: &mut Terminal<B>,
    mut editor: AutoCursorFormEditor<ApplicationData>,
) -> io::Result<()> {
    let mut suggestions_provider = ProductionSuggestionsProvider::new();

    loop {
        terminal.draw(|f| ui(f, &editor))?;

        if let Event::Key(key) = event::read()? {
            match handle_key_press(
                key.code,
                key.modifiers,
                &mut editor,
                &mut suggestions_provider,
            ) {
                Ok(should_continue) => {
                    if !should_continue {
                        break;
                    }
                }
                Err(e) => {
                    editor.set_debug_message(format!("Error: {e}"));
                }
            }
        }
    }

    Ok(())
}

fn ui(f: &mut Frame, editor: &AutoCursorFormEditor<ApplicationData>) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Min(8), Constraint::Length(12)])
        .split(f.area());

    let active_field_rect = render_enhanced_canvas(f, chunks[0], editor);

    // Render suggestions dropdown if active
    if let Some(input_rect) = active_field_rect {
        render_suggestions_dropdown(
            f,
            chunks[0],
            input_rect,
            &tui_canvas::canvas::theme::DefaultCanvasTheme,
            &editor.editor,
        );
    }

    render_status_and_help(f, chunks[1], editor);
}

fn render_enhanced_canvas(
    f: &mut Frame,
    area: ratatui::layout::Rect,
    editor: &AutoCursorFormEditor<ApplicationData>,
) -> Option<ratatui::layout::Rect> {
    render_canvas_default(f, area, &editor.editor)
}

fn render_status_and_help(
    f: &mut Frame,
    area: ratatui::layout::Rect,
    editor: &AutoCursorFormEditor<ApplicationData>,
) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(3), Constraint::Length(9)])
        .split(area);

    // Status bar with current field and cursor information
    let field_names = ["Fruit🍎", "Job💼", "Language💻", "Country🌍", "Color🎨"];
    let current_field_name = field_names
        .get(editor.current_field())
        .unwrap_or(&"Unknown");

    let mode_text = match editor.mode() {
        AppMode::Ins => "INSERT | (bar cursor)",
        AppMode::Nor => "NORMAL █ (block cursor)",
        AppMode::Sel => "VISUAL █ (blinking block)",
        _ => "NORMAL █ (block cursor)",
    };

    let suggestions_info = if editor.is_suggestions_active() {
        if editor.editor.ui_state().is_suggestions_loading() {
            " | ⏳ Loading suggestions...".to_string()
        } else if !editor.suggestions().is_empty() {
            format!(" | ✨ {} suggestions", editor.suggestions().len())
        } else {
            " | 🔍 No matches".to_string()
        }
    } else {
        "".to_string()
    };

    let status_text = format!(
        "-- {} -- {} | Field: {}{}",
        mode_text,
        editor.debug_message(),
        current_field_name,
        suggestions_info
    );

    let status = Paragraph::new(Line::from(Span::raw(status_text))).block(
        Block::default()
            .borders(Borders::ALL)
            .title("🚀 Production-Ready Smart Suggestions (Tab to activate → type to filter)"),
    );

    f.render_widget(status, chunks[0]);

    // Production help text
    let help_text = match editor.mode() {
        AppMode::Nor => {
            "🚀 PRODUCTION-READY SUGGESTIONS: Copy this architecture for your app!\n\
             Movement: j/k or ↑↓=fields, h/l or ←→=chars, gg/G=first/last, w/b/e=words\n\
             Actions: i/a/A=insert, v/V=visual, x/X=delete, ?=info, Enter=next field\n\
             Integration: Replace data sources with your APIs, databases, caches\n\
             Architecture: Non-blocking • Instant UI • Stale protection • Professional UX\n\
             🔑 Tab=activate suggestions → type to filter • Enter=select • Ready for: REST, GraphQL, SQL, Redis"
        }
        AppMode::Ins => {
            "🚀 INSERT MODE - Press Tab to activate suggestions, then type to filter!\n\
             Tab=activate suggestions • Type/Backspace=filter while active • Enter=select\n\
             Perfect for: Autocomplete, search dropdowns, data entry assistance\n\
             Navigation: arrows=move, Ctrl+arrows=words, Home/End=line edges\n\
             Copy this pattern for production: API calls, database queries, cache lookups"
        }
        AppMode::Sel => {
            "🚀 VISUAL MODE - Selection with suggestions support\n\
             Selection: hjkl/arrows=extend, w/b/e=word selection, Esc=normal\n\
             Professional editor experience with Tab-triggered autocomplete!"
        }
        _ => "🚀 Copy this suggestions architecture for your production app!"
    };

    let help = Paragraph::new(help_text)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title("📋 Production Integration Guide"),
        )
        .style(Style::default().fg(Color::Gray));

    f.render_widget(help, chunks[1]);
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Print demo information
    println!("🚀 Production-Ready Tab-Triggered Suggestions Demo");
    println!("✅ Press Tab to activate suggestions, then type to filter in real-time");
    println!("✅ Professional autocomplete architecture");
    println!("✅ Copy this pattern for your production application!");
    println!();
    println!("🏗️  Integration Ready For:");
    println!("   📡 REST APIs (reqwest, ureq)");
    println!("   🗄️  Databases (sqlx, diesel, mongodb)");
    println!("   🔍 Search Engines (elasticsearch, algolia, typesense)");
    println!("   💾 Caches (redis, memcached)");
    println!("   🌐 GraphQL APIs");
    println!("   🔗 gRPC Services");
    println!();
    println!("⚡ Key Features:");
    println!("   • Press Tab to activate suggestions dropdown");
    println!("   • Real-time filtering while suggestions are active");
    println!("   • Tab cycles through suggestions");
    println!("   • Professional-grade user experience");
    println!("   • Easy to integrate with any data source");
    println!();

    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    let data = ApplicationData::new();
    let mut editor = AutoCursorFormEditor::new(data);

    // Initialize with normal mode - library automatically sets block cursor
    editor.set_mode(AppMode::Nor);

    CursorManager::update_for_mode(AppMode::Nor)?;

    let res = run_app(&mut terminal, editor);

    CursorManager::reset()?;

    disable_raw_mode()?;
    execute!(
        terminal.backend_mut(),
        LeaveAlternateScreen,
        DisableMouseCapture
    )?;
    terminal.show_cursor()?;

    if let Err(err) = res {
        println!("{err:?}");
    }

    println!("🚀 Ready to integrate this architecture into your production app!");
    Ok(())
}
