use crate::context::BridgeContext;
use crate::mapping::{map_query_name_to_convex, payload_json_for_query, ConvexQueryRequest};
use crate::types::BridgeResult;
use crate::validation::validate_query_envelope;
use core_protocol::QueryEnvelope;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct QueryReadResult {
    pub workspace_id: Option<String>,
    pub request_id: String,
    pub trace_id: String,
}

pub fn build_query_request<T>(
    context: &BridgeContext,
    query: &QueryEnvelope<T>,
) -> BridgeResult<ConvexQueryRequest> {
    validate_query_envelope(context, query)?;
    Ok(ConvexQueryRequest {
        function_name: map_query_name_to_convex(&query.name).to_string(),
        deployment_id: context.deployment_id.clone(),
        project_id: context.project_id.clone(),
        workspace_id: context.workspace_id.clone(),
        request_id: context.request_id.clone(),
        trace_id: context.trace_id.clone(),
        actor_id: context.actor_id.clone(),
        payload_json: payload_json_for_query(context, &query.name),
    })
}
