use git::rpc::{
    proto as p, proto::archive_service_client::ArchiveServiceClient,
};
use session::Session;

use crate::{AppService, error::AppError, git::rpc_err};

impl AppService {
    pub async fn git_archive_tar(
        &self,
        ctx: &Session,
        wk_name: &str,
        repo_name: &str,
        options: Option<p::ArchiveOptions>,
    ) -> Result<p::ArchiveTarResponse, AppError> {
        let repo = self.git_require_member(ctx, wk_name, repo_name).await?;
        let mut client = ArchiveServiceClient::new(self.git.clone());
        let resp = client
            .archive_tar(tonic::Request::new(p::ArchiveTarRequest {
                repo_id: repo.id.to_string(),
                options,
            }))
            .await
            .map_err(rpc_err)?
            .into_inner();
        Ok(resp)
    }

    pub async fn git_archive_zip(
        &self,
        ctx: &Session,
        wk_name: &str,
        repo_name: &str,
        options: Option<p::ArchiveOptions>,
    ) -> Result<p::ArchiveZipResponse, AppError> {
        let repo = self.git_require_member(ctx, wk_name, repo_name).await?;
        let mut client = ArchiveServiceClient::new(self.git.clone());
        let resp = client
            .archive_zip(tonic::Request::new(p::ArchiveZipRequest {
                repo_id: repo.id.to_string(),
                options,
            }))
            .await
            .map_err(rpc_err)?
            .into_inner();
        Ok(resp)
    }
}
