// src/server/services/table_structure_service.rs
use tonic::{Request, Response, Status};
// Correct the import path for the TableStructureService trait
use common::proto::komp_ac::table_structure::table_structure_service_server::TableStructureService;
use common::proto::komp_ac::table_structure::{
    GetTableStructureRequest,
    TableStructureResponse,
};
use crate::table_structure::handlers::get_table_structure;
use sqlx::PgPool;

#[derive(Debug)]
pub struct TableStructureHandler {
    pub db_pool: PgPool,
}

impl TableStructureHandler {
    pub fn new(db_pool: PgPool) -> Self {
        Self { db_pool }
    }
}

#[tonic::async_trait]
impl TableStructureService for TableStructureHandler { // This line should now be correct
    async fn get_table_structure(
        &self,
        request: Request<GetTableStructureRequest>,
    ) -> Result<Response<TableStructureResponse>, Status> {
        let req_payload = request.into_inner();
        let response =
            get_table_structure(&self.db_pool, req_payload).await?;
        Ok(Response::new(response))
    }
}
