// src/state/pages/admin.rs

use ratatui::widgets::ListState;
use crate::state::pages::add_table::AddTableState;
use crate::state::pages::add_logic::AddLogicState;

// Define the focus states for the admin panel panes
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AdminFocus {
    #[default] // Default focus is on the profiles list
    ProfilesPane,
    InsideProfilesList,
    Tables,
    InsideTablesList,
    Button1,
    Button2,
    Button3,
}

#[derive(Default, Clone, Debug)]
pub struct AdminState {
    pub profiles: Vec<String>, // Holds profile names (used by non-admin view)
    pub profile_list_state: ListState, // Tracks navigation highlight (>) in profiles
    pub table_list_state: ListState,   // Tracks navigation highlight (>) in tables
    pub selected_profile_index: Option<usize>, // Index with [*] in profiles (persistent)
    pub selected_table_index: Option<usize>,   // Index with [*] in tables (persistent)
    pub current_focus: AdminFocus,     // Tracks which pane is focused
    pub add_table_state: AddTableState,
    pub add_logic_state: AddLogicState,
}

impl AdminState {
    /// Gets the index of the currently selected item.
    pub fn get_selected_index(&self) -> Option<usize> {
        self.profile_list_state.selected()
    }

    /// Gets the name of the currently selected profile.
    pub fn get_selected_profile_name(&self) -> Option<&String> {
        self.profile_list_state.selected().and_then(|i| self.profiles.get(i))
    }

    /// Populates the profile list and updates/resets the selection.
    pub fn set_profiles(&mut self, new_profiles: Vec<String>) {
        let current_selection_index = self.profile_list_state.selected();
        self.profiles = new_profiles;

        if self.profiles.is_empty() {
            self.profile_list_state.select(None);
        } else {
            let new_selection = match current_selection_index {
                Some(index) => Some(index.min(self.profiles.len() - 1)),
                None => Some(0),
            };
            self.profile_list_state.select(new_selection);
        }
    }

    /// Selects the next profile in the list, wrapping around.
    pub fn next(&mut self) {
        if self.profiles.is_empty() {
            self.profile_list_state.select(None);
            return;
        }
        let i = match self.profile_list_state.selected() {
            Some(i) => if i >= self.profiles.len() - 1 { 0 } else { i + 1 },
            None => 0,
        };
        self.profile_list_state.select(Some(i));
    }

    /// Selects the previous profile in the list, wrapping around.
    pub fn previous(&mut self) {
        if self.profiles.is_empty() {
            self.profile_list_state.select(None);
            return;
        }
        let i = match self.profile_list_state.selected() {
            Some(i) => if i == 0 { self.profiles.len() - 1 } else { i - 1 },
            None => self.profiles.len() - 1,
        };
        self.profile_list_state.select(Some(i));
    }

    /// Gets the index of the currently selected profile.
    pub fn get_selected_profile_index(&self) -> Option<usize> {
        self.profile_list_state.selected()
    }

    /// Gets the index of the currently selected table.
    pub fn get_selected_table_index(&self) -> Option<usize> {
        self.table_list_state.selected()
    }

    /// Selects a profile by index and resets table selection.
    pub fn select_profile(&mut self, index: Option<usize>) {
        self.profile_list_state.select(index);
        self.table_list_state.select(None);
    }

    /// Selects a table by index.
    pub fn select_table(&mut self, index: Option<usize>) {
        self.table_list_state.select(index);
    }

    /// Selects the next profile, wrapping around.
    /// `profile_count` should be the total number of profiles available.
    pub fn next_profile(&mut self, profile_count: usize) {
        if profile_count == 0 {
            return;
        }
        let i = match self.get_selected_profile_index() {
            Some(i) => {
                if i >= profile_count - 1 {
                    0
                } else {
                    i + 1
                }
            }
            None => 0,
        };
        self.select_profile(Some(i)); // Use the helper method
    }

    /// Selects the previous profile, wrapping around.
    /// `profile_count` should be the total number of profiles available.
    pub fn previous_profile(&mut self, profile_count: usize) {
        if profile_count == 0 {
            return;
        }
        let i = match self.get_selected_profile_index() {
            Some(i) => {
                if i == 0 {
                    profile_count - 1
                } else {
                    i - 1
                }
            }
            None => 0, // Or profile_count - 1 if you prefer wrapping from None
        };
        self.select_profile(Some(i)); // Use the helper method
    }

    /// Selects the next table, wrapping around.
    /// `table_count` should be the number of tables in the *currently selected* profile.
    pub fn next_table(&mut self, table_count: usize) {
        if table_count == 0 {
            return;
        }
        let i = match self.get_selected_table_index() {
            Some(i) => {
                if i >= table_count - 1 {
                    0
                } else {
                    i + 1
                }
            }
            None => 0,
        };
        self.select_table(Some(i));
    }

    /// Selects the previous table, wrapping around.
    /// `table_count` should be the number of tables in the *currently selected* profile.
    pub fn previous_table(&mut self, table_count: usize) {
        if table_count == 0 {
            return;
        }
        let i = match self.get_selected_table_index() {
            Some(i) => {
                if i == 0 {
                    table_count - 1
                } else {
                    i - 1
                }
            }
            None => 0, // Or table_count - 1
        };
        self.select_table(Some(i));
    }
}

