//! Session command types.
//!
//! Commands sent to `SessionManager` for session lifecycle operations.

use std::path::PathBuf;

use tokio::sync::oneshot;

use crate::error::SessionError;
use crate::session::SessionId;

/// Auto-input step for session initialization
#[derive(Debug, Clone)]
pub struct AutoInputStep {
    /// Data to send
    pub data: Vec<u8>,
    /// Delay in milliseconds before sending
    pub delay_ms: u64,
}

/// Commands sent to `SessionManager`
#[derive(Debug)]
pub enum SessionCommand {
    /// Create new session
    Create {
        /// Command to execute
        cmd: String,
        /// Command arguments
        args: Vec<String>,
        /// Working directory
        cwd: Option<PathBuf>,
        /// Custom branch name for worktree (None = auto-generate)
        branch_name: Option<String>,
        /// Terminal rows
        rows: u16,
        /// Terminal columns
        cols: u16,
        /// Response channel
        response_tx: oneshot::Sender<Result<SessionId, SessionError>>,
    },
    /// Terminate session
    Terminate {
        /// Session to terminate
        id: SessionId,
        /// Response channel
        response_tx: oneshot::Sender<Result<(), SessionError>>,
    },
    /// Send input to session
    SendInput {
        /// Target session
        id: SessionId,
        /// Input data
        data: Vec<u8>,
    },
    /// Resize session terminal
    Resize {
        /// Target session
        id: SessionId,
        /// New row count
        rows: u16,
        /// New column count
        cols: u16,
    },
    /// List all sessions
    List {
        /// Response channel
        response_tx: oneshot::Sender<Vec<crate::session::Session>>,
    },
    /// Create session from existing worktree (adopt)
    CreateFromWorktree {
        /// Path to the existing worktree
        worktree_path: PathBuf,
        /// Command to execute
        cmd: String,
        /// Command arguments
        args: Vec<String>,
        /// Terminal rows
        rows: u16,
        /// Terminal columns
        cols: u16,
        /// Response channel
        response_tx: oneshot::Sender<Result<SessionId, SessionError>>,
    },
    /// List available worktrees (not yet adopted as sessions)
    ListWorktrees {
        /// Response channel
        response_tx: oneshot::Sender<Vec<crate::worktree::WorktreeInfo>>,
    },
    /// Delete worktree (no active session)
    DeleteWorktree {
        /// Path to the worktree to delete
        path: PathBuf,
        /// Response channel
        response_tx: oneshot::Sender<Result<(), SessionError>>,
    },
    /// Git pull on worktree
    PullWorktree {
        /// Path to the worktree to pull
        path: PathBuf,
        /// Response channel
        response_tx: oneshot::Sender<Result<(), SessionError>>,
    },
    /// Git fetch on worktree (update remote tracking info)
    FetchWorktree {
        /// Path to the worktree to fetch
        path: PathBuf,
        /// Response channel
        response_tx: oneshot::Sender<Result<(), SessionError>>,
    },
    /// Get git status for a worktree
    GetWorktreeStatus {
        /// Path to the worktree
        path: PathBuf,
        /// Response channel
        response_tx: oneshot::Sender<Result<crate::worktree::GitWorktreeStatus, SessionError>>,
    },
    /// Refresh worktrees asynchronously (fetch + status)
    RefreshWorktreesAsync,
    /// Delete worktree asynchronously (no response channel)
    DeleteWorktreeAsync {
        /// Path to the worktree to delete
        path: PathBuf,
    },
    /// Git pull on worktree asynchronously (no response channel)
    PullWorktreeAsync {
        /// Path to the worktree to pull
        path: PathBuf,
    },
    /// Close terminated session (remove from TUI, cleanup worktree)
    CloseSession {
        /// Session to close
        id: SessionId,
        /// Response channel (returns worktree path if cleaned)
        response_tx: oneshot::Sender<Result<Option<PathBuf>, SessionError>>,
    },
    /// Create new session with auto-input sequence
    CreateWithAutoInput {
        /// Command to execute
        cmd: String,
        /// Command arguments
        args: Vec<String>,
        /// Working directory
        cwd: Option<PathBuf>,
        /// Custom branch name for worktree (None = auto-generate)
        branch_name: Option<String>,
        /// Terminal rows
        rows: u16,
        /// Terminal columns
        cols: u16,
        /// Auto-input steps to execute after session starts
        auto_input: Vec<AutoInputStep>,
        /// Response channel
        response_tx: oneshot::Sender<Result<SessionId, SessionError>>,
    },
    /// Fetch GitHub issues asynchronously
    FetchIssuesAsync,
    /// Generate action choices for a GitHub issue
    GenerateIssueActions {
        /// Issue number to generate actions for
        issue_number: u32,
    },
    /// Fetch today's usage cost from ccusage
    FetchCostAsync,
}
