use msyrs::download::{JPMaQSDownload, JPMaQSDownloadGetIndicatorArgs};
// use msyrs::download::jpmaqsdownload::*;
// use msyrs::utils::qdf::load::*;
// use msyrs::utils::qdf::dftools::*;
// use msyrs::utils::qdf::core::*;
use msyrs::utils as msyrs_utils;
use msyrs::utils::qdf as msyrs_qdf;
use polars::frame::DataFrame;

#[allow(dead_code)]
fn download_stuff() {
    println!("Authentication to DataQuery API");
    let mut jpamqs_download = JPMaQSDownload::default();

    println!("Checking connection to DataQuery API");
    jpamqs_download.check_connection().unwrap();

    println!("Retrieving catalogue of tickers");
    let start = std::time::Instant::now();
    let tickers = jpamqs_download.get_catalogue().unwrap();
    println!(
        "Retrieved catalogue with {} tickers in {:?}",
        tickers.len(),
        start.elapsed()
    );

    let num_ticks = 100;
    let sel_tickers: Vec<String> = tickers
        .iter()
        .take(num_ticks)
        .map(|s| s.to_string())
        .collect();
    // let mut df_deets = Vec::new();

    println!("Retrieving indicators for {} tickers", sel_tickers.len());

    let res_df = jpamqs_download
        .get_indicators_qdf(JPMaQSDownloadGetIndicatorArgs {
            tickers: sel_tickers.clone(),
            start_date: "2024-11-05".to_string(),
            ..Default::default()
        })
        .unwrap();

    println!(
        "Retrieved indicators for {} tickers in {:?}",
        sel_tickers.len(),
        start.elapsed()
    );

    if !msyrs_qdf::is_quantamental_dataframe(&res_df) {
        println!("DataFrame is not a quantamental DataFrame");
    } else {
        println!("DataFrame is a quantamental DataFrame");
    }
}

#[allow(dead_code)]
fn load_mega_df() -> DataFrame {
    let start = std::time::Instant::now();
    let st_pth = "E:\\Work\\jpmaqs-data\\data";

    let mega_df = msyrs_qdf::load::load_qdf_from_download_bank(
        st_pth,
        None,
        None,
        Some(vec![
            "AUD_EQXR_NSA",
            "USD_EQXR_NSA",
            "GBP_EQXR_NSA",
            "JPY_EQXR_NSA",
            "AUD_RIR_NSA",
            "USD_RIR_NSA",
            "GBP_RIR_NSA",
            "JPY_RIR_NSA",
        ]),
    )
    .unwrap();

    let end = start.elapsed();
    println!("Loaded Mega DataFrame in {:?}", end);
    mega_df
}

fn main() {
    nb();
}

#[allow(dead_code)]
fn nb() {
    let cids_dm = vec![
        "AUD", "CAD", "CHF", "EUR", "GBP", "JPY", "NOK", "NZD", "SEK", "USD",
    ];
    let cids_em = vec![
        "CLP", "COP", "CZK", "HUF", "IDR", "ILS", "INR", "KRW", "MXN", "PLN", "THB", "TRY", "TWD",
        "ZAR",
    ];

    // cids = cids_dm + cids_em
    let cids: Vec<String> = cids_dm
        .iter()
        .chain(cids_em.iter())
        .map(|s| s.to_string())
        .collect();

    let non_dux = ["IDR", "NZD"];
    #[allow(unused_variables)]
    let cids_dux: Vec<String> = cids
        .iter()
        .filter(|s| !non_dux.contains(&s.as_str()))
        .map(|s| s.to_string())
        .collect();
    let ecos = vec![
        "CPIC_SA_P1M1ML12",
        "CPIC_SJA_P3M3ML3AR",
        "CPIC_SJA_P6M6ML6AR",
        "CPIH_SA_P1M1ML12",
        "CPIH_SJA_P3M3ML3AR",
        "CPIH_SJA_P6M6ML6AR",
        "INFTEFF_NSA",
        "INTRGDP_NSA_P1M1ML12_3MMA",
        "INTRGDPv5Y_NSA_P1M1ML12_3MMA",
        "PCREDITGDP_SJA_D1M1ML12",
        "RGDP_SA_P1Q1QL4_20QMA",
        "RYLDIRS02Y_NSA",
        "RYLDIRS05Y_NSA",
        "PCREDITBN_SJA_P1M1ML12",
    ];
    let mkts = vec![
        "DU02YXR_NSA",
        "DU05YXR_NSA",
        "DU02YXR_VT10",
        "DU05YXR_VT10",
        "EQXR_NSA",
        "EQXR_VT10",
        "FXXR_NSA",
        "FXXR_VT10",
        "FXCRR_NSA",
        "FXTARGETED_NSA",
        "FXUNTRADABLE_NSA",
    ];

    let xcats: Vec<String> = ecos
        .iter()
        .chain(mkts.iter())
        .map(|s| s.to_string())
        .collect();

    let cids_str: Vec<&str> = cids.iter().map(AsRef::as_ref).collect();
    let xcats_str: Vec<&str> = xcats.iter().map(AsRef::as_ref).collect();
    let download_tickers = msyrs_utils::misc::create_interesecting_tickers(&cids_str, &xcats_str);

    let mut jpmaqs_client = JPMaQSDownload::default();
    let downloaded_df = jpmaqs_client
        .get_indicators_qdf(JPMaQSDownloadGetIndicatorArgs {
            tickers: download_tickers,
            ..Default::default()
        })
        .unwrap();

    println!("Downloaded DF:\n{:?}", downloaded_df.head(Some(10)));

    let eq_df = msyrs_qdf::reduce_dataframe(
        downloaded_df.clone(),
        None,
        Some(vec!["EQXR_NSA", "EQXR_VT10"]),
        None,
        // None,
        Some("2024-11-12"),
        None,
        false,
    )
    .unwrap();

    println!("{:?}", eq_df.head(Some(10)));

    let fx_df = msyrs_qdf::reduce_dataframe(
        downloaded_df,
        None,
        Some(vec![
            "FXXR_NSA",
            "FXXR_VT10",
            "FXCRR_NSA",
            "FXTARGETED_NSA",
            "FXUNTRADABLE_NSA",
        ]),
        None,
        // None,
        Some("2024-11-12"),
        None,
        true,
    )
    .unwrap();

    println!("{:?}", fx_df.head(Some(10)));

    let custom_df = msyrs_qdf::update_dataframe(&fx_df, &eq_df).unwrap();

    println!("{:?}", custom_df.head(Some(20)));
}
