use crate::{
    commands::{Cli, Config, Result},
    jobber::JobberGet,
    print_tip,
    views::{BillView, InvoiceView, create_bill_table},
};
use clap::Parser;

#[derive(Parser, Debug)]
pub(crate) struct ShowInvoice {
    /// Invoice number
    invoice_no: usize,
}

impl ShowInvoice {
    pub(crate) fn run(&self, cli: &Cli) -> Result<()> {
        println!();
        let config = Config::load()?;
        let database = cli.open_database()?;
        let invoice_no = self.invoice_no;
        println!("{}", InvoiceView::new(invoice_no, &database, cli.ansi())?);
        let invoice = database.get_invoice(invoice_no)?;

        let rows: Vec<_> = invoice
            .bills
            .iter()
            .enumerate()
            .map(|(n, b)| BillView::new(n, b, &database, cli.ansi()))
            .collect::<Result<Vec<_>>>()?;
        println!("{}", create_bill_table(&config, rows));
        print_tip!(cli);
        print_tip!(
            cli,
            "Use <s>jobber list invoice {}</> to get a deeper look.",
            invoice_no
        );
        print_tip!(
            cli,
            "Use <s>jobber export invoice {}</> to get a PDF.",
            invoice_no
        );
        Ok(())
    }
}
