use anyhow::Result;

use crate::git_context::{BranchInference, infer_work_item_from_branch};

/// Prints the work item identifier inferred from the current git branch name.
/// Designed for scripting and shell integration — outputs only the bare ID (e.g. "NRO-123")
/// with no decoration, suitable for piping into other commands.
/// Usage: `plane id` → "NRO-123"
#[derive(clap::Args)]
pub struct PrintWorkItemIdArgs;

/// Execution logic for the `plane id` subcommand
impl PrintWorkItemIdArgs {
    /// Reads the current git branch, extracts a work item identifier pattern,
    /// and prints it to stdout with no trailing decoration
    pub async fn execute(self) -> Result<()> {
        match infer_work_item_from_branch() {
            BranchInference::Resolved(identifier) => {
                println!("{}", identifier);
                Ok(())
            }
            BranchInference::NotInGitRepo => {
                anyhow::bail!("not in a git repository — cannot infer work item ID from branch");
            }
            BranchInference::NoIdentifierInBranch(branch_name) => {
                anyhow::bail!(
                    "branch '{}' does not contain a work item identifier (expected pattern like NRO-123)",
                    branch_name
                );
            }
        }
    }
}
