use std::sync::Arc;

use async_graphql::{Context, Object, Result, SimpleObject, ID};
use scel_core::App;

#[derive(SimpleObject, Clone)]
pub struct Download {
    pub id: ID,
    pub link: String,
    pub progress: Option<u32>,
    pub file_name: Option<String>,
}

pub struct QueryRoot;

#[Object]
impl QueryRoot {
    async fn get_download(&self, ctx: &Context<'_>, id: ID) -> Result<Option<Download>> {
        let app = ctx.data_unchecked::<Arc<App>>();

        match app.download_service.get_download(id.to_string()).await {
            Ok(Some(d)) => Ok(Some(Download {
                id: ID::from(d.id.expect("ID could not be found")),
                progress: None,
                link: d.link,
                file_name: None,
            })),
            Ok(None) => Ok(None),
            Err(e) => Err(e.into()),
        }
    }
}
