use snafu::ResultExt;
use std::sync::Arc;

use super::*;
use crate::client::GeminiClient;

/// A handle to a file on the Gemini API.
pub struct FileHandle {
    inner: super::model::File,
    client: Arc<GeminiClient>,
}

impl FileHandle {
    pub(crate) fn new(client: Arc<GeminiClient>, inner: super::model::File) -> Self {
        Self { inner, client }
    }

    pub fn name(&self) -> &str {
        &self.inner.name
    }

    /// Get the file metadata.
    pub fn get_file_meta(&self) -> &super::model::File {
        &self.inner
    }

    /// Delete the file.
    pub async fn delete(self) -> Result<(), (Self, Error)> {
        match self.client.delete_file(&self.inner.name).await.context(ClientSnafu) {
            Ok(_) => Ok(()),
            Err(e) => Err((self, e)),
        }
    }

    /// Download the file.
    ///
    /// Work only for files that was generated by Gemini.
    pub async fn download(&self) -> Result<Vec<u8>, Error> {
        self.client.download_file(self.name()).await.context(ClientSnafu)
    }
}
