use mangadex_api_schema::v5::RelatedAttributes;
use mangadex_api_types::{Language, ReferenceExpansionResource};
use std::time::Duration;
use url::Url;
use mangadex_api::MangaDexClient;
use reqwest::{
    header::{HeaderMap, HeaderValue, USER_AGENT},
    Client,
};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let mut default_header = HeaderMap::new();
        default_header.insert(USER_AGENT, HeaderValue::from_str("myApp v0")?);
    let reqwest_client = Client::builder()
        .default_headers(default_header)
                .timeout(Duration::from_secs(15))
        .build()?;
    let client = MangaDexClient::new(reqwest_client);
        
    let res = client
        .manga()
        .get()
        .title("Konsei ga")
                .include(ReferenceExpansionResource::Author)
                .include(ReferenceExpansionResource::Artist)
        .send()
        .await?;
    let not_found = String::from("Not found");
        let mut index = 1;
    for manga in res.data {
                let title = manga
            .attributes
            .title
            .get(&Language::English)
            .unwrap_or(¬_found);
        println!("{index} ~ {title}");
                let author = manga
            .find_first_relationships(mangadex_api_types::RelationshipType::Author)
            .and_then(|e| {
                e.attributes.clone().map(|rel| match rel {
                    RelatedAttributes::Author(a) => a.name,
                    _ => not_found.clone(),
                })
            })
            .unwrap_or(not_found.clone());
        println!("\tAuthor: {author}");
                let artist = manga
            .find_first_relationships(mangadex_api_types::RelationshipType::Artist)
            .and_then(|e| {
                e.attributes.clone().map(|rel| match rel {
                    RelatedAttributes::Author(a) => a.name,
                    _ => not_found.clone(),
                })
            })
            .unwrap_or(not_found.clone());
        if artist != author {
            println!("\tArtist: {artist}");
        }
                let title_link =
            Url::parse("https://mangadex.org/title/")?.join(manga.id.to_string().as_str())?;
        println!("\tLink: {title_link}");
        println!();
        index += 1;
    }
    Ok(())
}