// src/server/services/tables_data_service.rs

use tonic::{Request, Response, Status};
// Add these imports
use tokio::sync::mpsc;
use crate::indexer::IndexCommand;

use common::proto::komp_ac::tables_data::tables_data_server::TablesData;
use common::proto::komp_ac::common::CountResponse;
use common::proto::komp_ac::tables_data::{
    PostTableDataRequest, PostTableDataResponse,
    PutTableDataRequest, PutTableDataResponse,
    DeleteTableDataRequest, DeleteTableDataResponse,
    GetTableDataRequest, GetTableDataResponse,
    GetTableDataCountRequest, GetTableDataByPositionRequest,
};
use crate::tables_data::handlers::{post_table_data, put_table_data, delete_table_data, get_table_data, get_table_data_count, get_table_data_by_position};
use sqlx::PgPool;

#[derive(Debug)]
pub struct TablesDataService {
    pub db_pool: PgPool,
    // MODIFIED: Add the sender field
    pub indexer_tx: mpsc::Sender<IndexCommand>,
}

#[tonic::async_trait]
impl TablesData for TablesDataService {
    async fn post_table_data(
        &self,
        request: Request<PostTableDataRequest>,
    ) -> Result<Response<PostTableDataResponse>, Status> {
        let request = request.into_inner();
        // MODIFIED: Pass the indexer_tx to the handler
        let response = post_table_data(
            &self.db_pool,
            request,
            &self.indexer_tx,
        )
        .await?;
        Ok(Response::new(response))
    }

    async fn put_table_data(
        &self,
        request: Request<PutTableDataRequest>,
    ) -> Result<Response<PutTableDataResponse>, Status> {
        let request = request.into_inner();
        let response = put_table_data(
            &self.db_pool,
            request,
            &self.indexer_tx,
        )
        .await?;
        Ok(Response::new(response))
    }

    // ...and delete_table_data
    async fn delete_table_data(
        &self,
        request: Request<DeleteTableDataRequest>,
    ) -> Result<Response<DeleteTableDataResponse>, Status> {
        let request = request.into_inner();
        // TODO: Update delete_table_data handler to accept and use indexer_tx
        let response = delete_table_data(&self.db_pool, request).await?;
        Ok(Response::new(response))
    }

    async fn get_table_data(
        &self,
        request: Request<GetTableDataRequest>,
    ) -> Result<Response<GetTableDataResponse>, Status> {
        let request = request.into_inner();
        let response = get_table_data(&self.db_pool, request).await?;
        Ok(Response::new(response))
    }

    async fn get_table_data_count(
        &self,
        request: Request<GetTableDataCountRequest>,
    ) -> Result<Response<CountResponse>, Status> {
        let request = request.into_inner();
        let response = get_table_data_count(&self.db_pool, request).await?;
        Ok(Response::new(response))
    }

    async fn get_table_data_by_position(
        &self,
        request: Request<GetTableDataByPositionRequest>,
    ) -> Result<Response<GetTableDataResponse>, Status> {
        let request = request.into_inner();
        let response = get_table_data_by_position(&self.db_pool, request).await?;
        Ok(Response::new(response))
    }
}
