//! Staging and unstaging actions.
//!
//! Handles optimistic updates for staging/unstaging files.

use crate::app::{actions::Action, state::AppState};
use std::collections::HashSet;

/// Handle staging/unstaging actions.
pub fn handle_staging(state: &mut AppState, action: &Action) -> bool {
    match action {
        Action::StageSelectedFile => {
            let paths: HashSet<String> = if state.selected_files.is_empty() {
                if !state.status_selected_path.is_empty() {
                    [state.status_selected_path.clone()].into_iter().collect()
                } else {
                    HashSet::new()
                }
            } else {
                state.selected_files.clone()
            };
            for entry in state.status_entries.iter_mut() {
                if paths.contains(&entry.path) {
                    // Stage the file - but don't clear unstaged flag
                    // A file can have BOTH staged and unstaged changes (staged, then modified again)
                    // The actual git status refresh will set the correct state
                    entry.staged = true;
                    // Don't set entry.unstaged = false here - preserve existing unstaged state
                    // Status refresh will update it correctly based on actual git state
                }
            }
            true
        }
        Action::UnstageSelectedFile => {
            let paths: HashSet<String> = if state.selected_files.is_empty() {
                if !state.status_selected_path.is_empty() {
                    [state.status_selected_path.clone()].into_iter().collect()
                } else {
                    HashSet::new()
                }
            } else {
                state.selected_files.clone()
            };
            for entry in state.status_entries.iter_mut() {
                if paths.contains(&entry.path) {
                    // Unstage the file - but don't assume unstaged = true
                    // The file might have been staged but not have unstaged changes
                    // The actual git status refresh will set the correct state
                    entry.staged = false;
                    // Don't set entry.unstaged = true here - preserve existing state
                    // Status refresh will update it correctly based on actual git state
                }
            }
            true
        }
        Action::StageAllFiles => {
            for entry in state.status_entries.iter_mut() {
                // Stage files that are unstaged or untracked
                // Don't clear unstaged flag - file might have both staged and unstaged changes
                // Status refresh will update correctly
                if entry.unstaged || (!entry.staged && !entry.unstaged) {
                    entry.staged = true;
                    // Don't set entry.unstaged = false - preserve existing state
                }
            }
            true
        }
        Action::UnstageAllFiles => {
            for entry in state.status_entries.iter_mut() {
                // Unstage files that are staged
                // Don't assume unstaged = true - status refresh will set correct state
                if entry.staged {
                    entry.staged = false;
                    // Don't set entry.unstaged = true - preserve existing state
                }
            }
            true
        }
        _ => false,
    }
}

