//! Fuzzy-selects a repo and prints its path for shell `cd` integration.

use crate::error::ReportalError;
use crate::reportal_config::{ReportalConfig, TagFilter};
use crate::reportal_commands::path_display::{self, SelectedPathDisplayParams};
use crate::reportal_commands::repo_selection::{self, SelectedRepoParams};
use crate::reportal_commands::terminal_identity_emit::{
    self, TerminalIdentityEmitParams,
};
use crate::terminal_style;

/// All parameters needed to run the jump command.
pub struct JumpCommandParams<'a> {
    /// Which repos to show in the fuzzy finder.
    pub tag_filter: TagFilter,
    /// If non-empty, skip the fuzzy finder and jump directly.
    pub direct_alias: &'a str,
    /// If non-empty, override the tab title for this session.
    pub title_override: &'a str,
}

/// Prints the selected repo's resolved path to stdout; the shell
/// wrapper function (`rj`) reads this and runs `cd`.
///
/// If a direct alias is given, skips the fuzzy finder entirely.
/// The raw path always goes to stdout for the shell function;
/// an optional styled confirmation goes to stderr based on config.
/// Also emits OSC escape sequences for tab title and background color.
pub fn run_jump(jump_params: &JumpCommandParams<'_>) -> Result<(), ReportalError> {
    let loaded_config = ReportalConfig::load_from_disk()?;

    let selection_params = SelectedRepoParams {
        loaded_config: &loaded_config,
        direct_alias: jump_params.direct_alias,
        tag_filter: &jump_params.tag_filter,
        prompt_label: "Jump to repo",
    };
    let selected = repo_selection::select_repo(&selection_params)?;

    terminal_identity_emit::emit_repo_terminal_identity(&TerminalIdentityEmitParams {
        selected_alias: selected.repo_alias(),
        selected_repo: selected.repo_config(),
        title_override: jump_params.title_override,
    });

    let resolved_path = selected.repo_config().resolved_path();
    let formatted_path = loaded_config.path_display_format().format_path(&resolved_path);

    terminal_style::write_stdout(&formatted_path.clone());

    path_display::print_selected_path_if_visible(&SelectedPathDisplayParams {
        loaded_config: &loaded_config,
        resolved_path: &resolved_path,
    });

    Ok(())
}
