// src/components/form/form.rs
use crate::components::common::autocomplete;
use crate::config::colors::themes::Theme;
use canvas::canvas::{CanvasState, render_canvas, HighlightState};
use crate::state::pages::form::FormState;
use ratatui::{
    layout::{Alignment, Constraint, Direction, Layout, Margin, Rect},
    style::Style,
    widgets::{Block, Borders, Paragraph},
    Frame,
};

pub fn render_form(
    f: &mut Frame,
    area: Rect,
    form_state: &FormState,
    fields: &[&str],
    current_field_idx: &usize,
    inputs: &[&String],
    table_name: &str,
    theme: &Theme,
    is_edit_mode: bool,
    highlight_state: &HighlightState,
    total_count: u64,
    current_position: u64,
) {
    let card_title = format!(" {} ", table_name);

    let adresar_card = Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme.border))
        .title(card_title)
        .style(Style::default().bg(theme.bg).fg(theme.fg));

    f.render_widget(adresar_card, area);

    let inner_area = area.inner(Margin {
        horizontal: 1,
        vertical: 1,
    });

    let main_layout = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(1), Constraint::Min(1)])
        .split(inner_area);

    let count_position_text = if total_count == 0 && current_position == 1 {
        "Total: 0 | New Entry".to_string()
    } else if current_position > total_count && total_count > 0 {
        format!("Total: {} | New Entry ({})", total_count, current_position)
    } else if total_count == 0 && current_position > 1 {
        format!("Total: 0 | New Entry ({})", current_position)
    } else {
        format!(
            "Total: {} | Position: {}/{}",
            total_count, current_position, total_count
        )
    };
    
    let count_para = Paragraph::new(count_position_text)
        .style(Style::default().fg(theme.fg))
        .alignment(Alignment::Left);
    f.render_widget(count_para, main_layout[0]);

    // Use the canvas library's render_canvas function
    let active_field_rect = render_canvas(
        f,
        main_layout[1],
        form_state,
        theme,
        is_edit_mode,
        highlight_state,
    );

    // --- RENDER RICH AUTOCOMPLETE ONLY ---
    if form_state.autocomplete_active {
        if let Some(active_rect) = active_field_rect {
            // Get selected index directly from form_state
            let selected_index = form_state.selected_suggestion_index;

            // Only render rich suggestions (your Hit objects)
            if let Some(rich_suggestions) = form_state.get_rich_suggestions() {
                if !rich_suggestions.is_empty() {
                    autocomplete::render_hit_autocomplete_dropdown(
                        f,
                        active_rect,
                        f.area(),
                        theme,
                        rich_suggestions,
                        selected_index,
                        form_state,
                    );
                }
            }
            // Removed simple suggestions - we only use rich ones now!
        }
    }
}
