use crate::{args::Args, utils::time::get_system_creation_date};
use chrono::{Local, NaiveDate, TimeZone};
use clap::Parser;
use colored::*;
use cosmoutils::modules::msg::{error};
use std::process::exit;
pub fn count_time_to_target(target_date: &str) -> Result<(), Box<dyn std::error::Error>> {
    let args = Args::parse();

    let start_datetime = get_system_creation_date()?; // full timestamp
    let now = Local::now();

    // Parse target date at midnight
    let target = match NaiveDate::parse_from_str(target_date, "%Y-%m-%d") {
        Ok(d) => Local
            .from_local_datetime(&d.and_hms_opt(0, 0, 0).unwrap())
            .unwrap(),
        Err(_) => {
            error("Invalid date format (YYYY-MM-DD)");
            exit(1);
        }
    };

    if target < now {
        println!("Target date is before today!");
        return Ok(());
    }

    let total_days = target.signed_duration_since(start_datetime).num_days() + 1;
    let days_passed = now.signed_duration_since(start_datetime).num_days();
    let days_left = total_days - days_passed;

    if args.no_colors {
        println!("{days_left} Days");
    } else {
        println!(
            "{} {}",
            days_left.to_string().cyan().bold(),
            "Days".cyan().bold()
        );
    }

    Ok(())
}
