use cloud_terrastodon_azure_types::prelude::RoleEligibilitySchedule;
use cloud_terrastodon_command::CacheKey;
use cloud_terrastodon_command::CommandBuilder;
use cloud_terrastodon_command::CommandKind;
use eyre::Result;
use serde::Deserialize;
use std::path::PathBuf;

pub async fn fetch_my_role_eligibility_schedules() -> Result<Vec<RoleEligibilitySchedule>> {
    let url = "https://management.azure.com/providers/Microsoft.Authorization/roleEligibilitySchedules?api-version=2020-10-01&$filter=asTarget()";
    let mut cmd = CommandBuilder::new(CommandKind::AzureCLI);
    cmd.args(["rest", "--method", "GET", "--url", url]);
    cmd.cache(CacheKey::new(PathBuf::from_iter([
        "az",
        "rest",
        "GET",
        "roleEligibilitySchedules",
    ])));

    #[derive(Deserialize)]
    struct Response {
        value: Vec<RoleEligibilitySchedule>,
    }

    let mut result: Result<Response, _> = cmd.run().await;
    if result.is_err() {
        // single retry - sometimes this returns a gateway error
        result = cmd.run().await;
    }
    Ok(result?.value)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::prelude::test_helpers::expect_aad_premium_p2_license;

    #[tokio::test]
    async fn it_works() -> Result<()> {
        let Some(found) =
            expect_aad_premium_p2_license(fetch_my_role_eligibility_schedules().await).await?
        else {
            return Ok(());
        };
        assert!(!found.is_empty());
        for x in found {
            println!("- {x}");
        }
        Ok(())
    }
}
