use crate::utils::bdates::get_bdates_list_with_freq;
use crate::utils::bdates::BDateFreq;
use chrono::NaiveDate;
use chrono::{Datelike, Weekday};
use polars::prelude::*;
use std::collections::HashMap;
use std::error::Error;

/// Get the minimum and maximum dates from a date column in a DataFrame.
pub fn get_min_max_real_dates(
    df: &DataFrame,
    date_col: &str,
) -> Result<(NaiveDate, NaiveDate), Box<dyn Error>> {
    let date_series = df.column(date_col)?;
    if let DataType::Date = date_series.dtype() {
        // Convert the `date` series to an i32 (days since 1970-01-01)
        let date_as_days = date_series.cast(&DataType::Int32)?;
        let min_days = date_as_days.i32()?.min().ok_or("No minimum value found")?;
        let max_days = date_as_days.i32()?.max().ok_or("No maximum value found")?;

        // Convert the days back to `NaiveDate`
        let min_date = NaiveDate::from_ymd_opt(1970, 1, 1)
            .unwrap()
            .checked_add_signed(chrono::Duration::days(min_days as i64))
            .ok_or("Invalid minimum date")?;
        let max_date = NaiveDate::from_ymd_opt(1970, 1, 1)
            .unwrap()
            .checked_add_signed(chrono::Duration::days(max_days as i64))
            .ok_or("Invalid maximum date")?;

        Ok((min_date, max_date))
    } else {
        Err(Box::new(polars::error::PolarsError::ComputeError(
            "The column is not of Date type".into(),
        )))
    }
}

pub fn get_bdates_series_default(
    start_date: String,
    end_date: String,
    freq: Option<String>,
) -> Result<Series, Box<dyn Error>> {
    let freq = freq.unwrap_or_else(|| "D".to_string());
    let freq = BDateFreq::from_str(&freq)?;
    get_bdates_series(start_date, end_date, freq)
}

/// Get the business dates between two dates as a Series.
pub fn get_bdates_series(
    start_date: String,
    end_date: String,
    freq: BDateFreq,
) -> Result<Series, Box<dyn Error>> {
    let bdates_list = get_bdates_list_with_freq(&start_date, &end_date, freq)?;
    let bdates_series = Series::new("bdates".into(), bdates_list);
    Ok(bdates_series)
}

/// Get the business dates from a date column in a DataFrame.
/// Identify business days, bucket them by period, and pick the first available date from each period.
pub fn get_bdates_from_col(date_col: &Series, freq: &str) -> Result<Series, Box<dyn Error>> {
    // Ensure the column is of Date type
    if date_col.dtype() != &DataType::Date {
        return Err("The column is not of Date type".into());
    }

    // Step 1: Identify business days (exclude weekends)
    let date_as_days = date_col.cast(&DataType::Int32)?;
    let business_days: Vec<NaiveDate> = date_as_days
        .i32()?
        .into_iter()
        .filter_map(|opt_days| {
            opt_days.map(|days| {
                NaiveDate::from_ymd_opt(1970, 1, 1).unwrap() + chrono::Duration::days(days as i64)
            })
        })
        .filter(|date| {
            // Exclude weekends (Saturday and Sunday)
            let weekday = date.weekday();
            weekday != Weekday::Sat && weekday != Weekday::Sun
        })
        .collect();

    // Step 2: Bucket dates by period
    let mut buckets: HashMap<String, Vec<NaiveDate>> = HashMap::new();
    for date in &business_days {
        let bucket_key = match freq {
            "D" => date.format("%Y-%m-%d").to_string(),
            "W" => format!("{}-W{:02}", date.year(), date.iso_week().week()),
            "M" => date.format("%Y-%m").to_string(),
            "Q" => format!("{}-Q{}", date.year(), (date.month() - 1) / 3 + 1),
            "A" => date.year().to_string(),
            _ => return Err("Invalid frequency specified".into()),
        };
        buckets.entry(bucket_key).or_default().push(*date);
    }

    // Step 3: Pick the first available date from each bucket
    let mut selected_dates: Vec<NaiveDate> = Vec::new();
    for (_, mut dates) in buckets {
        dates.sort(); // Ensure dates are sorted within the bucket
        if let Some(first_date) = dates.first() {
            selected_dates.push(*first_date);
        }
    }

    // Step 4: Convert selected dates back to a Series of Date type
    let bdates_series = Series::new(
        "bdates".into(),
        selected_dates
            .into_iter()
            .map(|date| date.format("%Y-%m-%d").to_string()) // Format as strings
            .collect::<Vec<String>>(),
    )
    .cast(&DataType::Date)?; // Cast to Date type

    Ok(bdates_series)
}
