use crate::api::RpcService;
use crate::proto::soma::BatchGetObjectsRequest;
use crate::proto::soma::BatchGetObjectsResponse;
use crate::proto::soma::BatchGetTransactionsRequest;
use crate::proto::soma::BatchGetTransactionsResponse;
use crate::proto::soma::GetCheckpointRequest;
use crate::proto::soma::GetCheckpointResponse;
use crate::proto::soma::GetEpochRequest;
use crate::proto::soma::GetEpochResponse;
use crate::proto::soma::GetObjectRequest;
use crate::proto::soma::GetObjectResponse;
use crate::proto::soma::GetServiceInfoRequest;
use crate::proto::soma::GetServiceInfoResponse;
use crate::proto::soma::GetTransactionRequest;
use crate::proto::soma::GetTransactionResponse;
use crate::proto::soma::ledger_service_server::LedgerService;

mod get_checkpoint;
mod get_epoch;
mod get_object;
mod get_service_info;
mod get_transaction;

#[tonic::async_trait]
impl LedgerService for RpcService {
    async fn get_service_info(
        &self,
        _request: tonic::Request<GetServiceInfoRequest>,
    ) -> Result<tonic::Response<GetServiceInfoResponse>, tonic::Status> {
        get_service_info::get_service_info(self).map(tonic::Response::new).map_err(Into::into)
    }

    async fn get_object(
        &self,
        request: tonic::Request<GetObjectRequest>,
    ) -> Result<tonic::Response<GetObjectResponse>, tonic::Status> {
        get_object::get_object(self, request.into_inner())
            .map(tonic::Response::new)
            .map_err(Into::into)
    }

    async fn batch_get_objects(
        &self,
        request: tonic::Request<BatchGetObjectsRequest>,
    ) -> Result<tonic::Response<BatchGetObjectsResponse>, tonic::Status> {
        get_object::batch_get_objects(self, request.into_inner())
            .map(tonic::Response::new)
            .map_err(Into::into)
    }

    async fn get_transaction(
        &self,
        request: tonic::Request<GetTransactionRequest>,
    ) -> Result<tonic::Response<GetTransactionResponse>, tonic::Status> {
        get_transaction::get_transaction(self, request.into_inner())
            .map(tonic::Response::new)
            .map_err(Into::into)
    }

    async fn batch_get_transactions(
        &self,
        request: tonic::Request<BatchGetTransactionsRequest>,
    ) -> Result<tonic::Response<BatchGetTransactionsResponse>, tonic::Status> {
        get_transaction::batch_get_transactions(self, request.into_inner())
            .map(tonic::Response::new)
            .map_err(Into::into)
    }

    async fn get_checkpoint(
        &self,
        request: tonic::Request<GetCheckpointRequest>,
    ) -> Result<tonic::Response<GetCheckpointResponse>, tonic::Status> {
        get_checkpoint::get_checkpoint(self, request.into_inner())
            .map(tonic::Response::new)
            .map_err(Into::into)
    }

    async fn get_epoch(
        &self,
        request: tonic::Request<GetEpochRequest>,
    ) -> Result<tonic::Response<GetEpochResponse>, tonic::Status> {
        get_epoch::get_epoch(self, request.into_inner())
            .map(tonic::Response::new)
            .map_err(Into::into)
    }
}
