use super::RezonCommand;
use crate::{
	config::rezon_config::RezonConfig,
	tui::{clean_console, indent},
};
use async_trait::async_trait;
use clap::{arg, Arg, ArgAction, ArgMatches, Command};
use colorful::{Color, Colorful};
use spinach::Spinach;
use std::{thread, time};

// SAMPLE command
// rezon report --days 4

pub struct Report {}

#[async_trait]
impl RezonCommand for Report {
	fn cmd() -> clap::Command {
		Command::new("report")
			.about("Generate a report of the model's usage")
			.arg(
				Arg::new("days")
					.short('d')
					.long("days")
					.help("Specify the number of days to generate the report for. Defaults to the last 30 days."),
			)
			.arg(
				Arg::new("requests")
					.short('r')
					.long("requests")
					.help("Specify the number of requests to generate the report for. Overrides the --days option."),
			)
	}

	async fn execute(_: Option<RezonConfig>, _: ArgMatches) -> Result<(), crate::cli::CliError<'static>> {
		println!();

		let loading = Spinach::new("Downloading...");
		let file_path = "/Users/JRDarrenbaldwin/rezon/reports.json";

		let contents = std::fs::read_to_string(&file_path).unwrap();

		// Sleep a little to show loading animation, etc
		let timeout = time::Duration::from_millis(1500);
		thread::sleep(timeout);

		loading.stop();

		// Clear the console
		clean_console();

		println!();

		// Show the user their report json inside of the terminal but also tell them they can open a file inside of the /rezon/report.json file
		println!(
			"{} {}",
			format!("{}", "➜".color(Color::Purple1a)).bold(),
			format!("{}", "Report generated:".bold())
		);

		println!("{}", indent(1));

		println!("{}", contents);

		println!("{}", indent(1));

		println!(
			"{} {}",
			"Open the report file with:".bold(),
			"open ~/rezon/reports.json".color(Color::Purple1a).bold()
		);

		Ok(())
	}
}

// We dont use these right now but they may be used later:

// fn read_json_file(file_path: &str) -> Result<String, std::io::Error> {
//     let mut file = fs::File::open(file_path)?;
//     let mut json_data = String::new();
//     file.read_to_string(&mut json_data)?;
//     Ok(json_data)
// }

// fn write_json_to_new_file(json_data: &str, file_name: &str) -> Result<(), std::io::Error> {
//     let mut new_file = fs::File::create(file_name)?;
//     new_file.write_all(json_data.as_bytes())?;
//     new_file.flush()?;
//     Ok(())
// }
