use crate::{
    commands::{Cli, Config, Error, Result},
    jobber::JobberGet,
    views::WorkerView,
};
use clap::Parser;

#[derive(Parser, Debug)]
pub(crate) struct ShowWorker {
    /// Worker's identifier
    id: Option<String>,
}

impl ShowWorker {
    pub(crate) fn run(&self, cli: &Cli) -> Result<()> {
        println!();
        let config = Config::load()?;
        let database = cli.open_database()?;
        let id = if let Some(id) = &self.id {
            id
        } else if let Some(current) = config.current_job {
            &database.get_job(current)?.worker
        } else {
            Err(Error::NoCurrentJob)?
        };
        println!("{}", WorkerView::new(id, &config, &database, cli.ansi())?);

        Ok(())
    }
}
