use crate::{
    commands::{Cli, Config, Error, Result, current_job},
    entities::DateTime,
    jobber::{Deleted, JobberGet, JobberIter},
    print_tip,
    views::create_overview_table,
};
use clap::Parser;
use color_print::cprintln;

/// Show info about this application.
#[derive(Parser, Default, Debug)]
pub(crate) struct Info {
    /// suppress program information
    #[clap(long)]
    no_info: bool,
    /// suppress database statistics
    #[clap(long)]
    no_stats: bool,
}

impl Info {
    pub(crate) fn run(&self, cli: &Cli) -> Result<()> {
        if !self.no_info {
            let version = env!("CARGO_PKG_VERSION");
            cprintln!(
                "
    ░▒░▒░░░▓▒▓▓▓▓▓▓▓▓▓▓▓▓▓█▓▓▓░            8                                 
   ░▒░ ░  ▒░▒▓▓▓▓▓▓▓▓▓▓▒▓▓▓▓▓▓▓            8  eeeee eeeee  eeeee  eeee eeeee 
      ▒   ░▒▒▓▓▓▒▓▓▓▓▓▓▒▒▒▒▓▓▓▓▓▓▒░        8e 8  88 8   8  8   8  8    8   8 
    ░▒       ░▒░     ▒▓▓▒▒ ▒▒▓▒▓▓ ░▒       88 8   8 8eee8e 8eee8e 8eee 8eee8e
    ▒▒         ▓▓     ░ ▒▒▒▒░░░▓▒▒ ░   e   88 8   8 88   8 88   8 88   88   8
      ░    ░░░░▓▓▓▓▒▓▓▓▓▒▒░░ ░▒ ░      8eee88 8eee8 88eee8 88eee8 88ee 88   8
      ░  ░▒▒▒ ░▓▓▓░▓▓▓▓▓▒░    ▒░░  ░  
   ░ ▒▒   ▒░░     ░▓ ▓▓▒▒   ░▒  ░         A minimalistic open source
     ▒▒░            ░ ░▒ ▒▓▒ ░ ░          work time management system
     ░                    ░▒░░            for the command line.
  ░           ░         ▒▒▒▒░▒░           
  ░  ▒▒ ▒░░░▒▒░░▒▒▓▒▒░░░▒▒▒▒░▓▒░          Version: <i>{version}</i>
    ▒░ ▒░▒▒▓▒▒░▒ ▒▓▓▒▒▒░▒▓▓▓▒▒▓▓░         
   ░░▒░░ ▒▒▒▒▒▒▒▒▒▓▒▒▓▓▒▒▒▒▓▓▓▓           Copyright (C) 2026 Patrick Hoffmann
"
            );

            if is_marx_birthday() {
                println!("Today is Karl Marx' birthday!\n")
            }

            if is_marx_day_of_death() {
                println!("Today is Karl Marx' day of death.\n")
            }
        }

        let config = Config::load()?;
        match cli.open_database() {
            Ok(database) => {
                if !self.no_stats {
                    println!("{}", create_overview_table(&config, &database, cli.ansi())?);
                }
                match current_job(&config, &database) {
                    Ok(job_id) => {
                        let job = database.get_job(job_id)?;
                        print_tip!(cli);
                        print_tip!(
                            cli,
                            "Use <s>jobber start</> to begin work as <s>{}</> in job <s>{}</> for client <s>{}</>.",
                            job_id,
                            database.get_full_worker_name(&job.worker)?,
                            database.get_full_client_name(&job.client)?
                        );
                        print_tip!(cli, "Use <s>jobber select</> to switch job.");
                    }
                    Err(_) => {
                        if database.iter_workers(Deleted::All).count() == 0 {
                            cprintln!("No workers.");
                            print_tip!(cli);
                            print_tip!(cli, "Use <s>jobber create worker</> to create a worker.");
                        } else if database.iter_clients(Deleted::All).count() == 0 {
                            cprintln!("No clients.");
                            print_tip!(cli);
                            print_tip!(cli, "Use <s>jobber create client</> to create a client.");
                        } else {
                            cprintln!("No current job.");
                            print_tip!(cli);
                            print_tip!(cli, "Use <s>jobber create job</> to create a new job.");
                            print_tip!(cli, "Use <s>jobber select</> to <s>select</> a job.");
                        }
                    }
                }
                if database.get_company().is_err() {
                    print_tip!(
                        cli,
                        "Use <s>jobber create company</> to set your company information."
                    );
                }
                print_tip!(cli, "Run <s>jobber help</> to get more advice.");
            }
            Err(Error::Io(_)) => {
                println!("Database could not be found!");
                print_tip!(cli);
                print_tip!(cli, "Use <s>jobber init</> to create a database.");
            }
            Err(err) => {
                return Err(err);
            }
        }

        Ok(())
    }
}

fn is_marx_birthday() -> bool {
    DateTime::now().format("%m-%d").to_string() == "05-05"
}

fn is_marx_day_of_death() -> bool {
    DateTime::now().format("%m-%d").to_string() == "03-17"
}
