//! Runtime boundary for client-pressure worker orchestration.

use anyhow::Result;
use async_trait::async_trait;
use sqlx::postgres::PgPool;
use std::collections::HashMap;

pub const DEFAULT_PRESSURE_WORKER_INTERVAL_SECS: u64 = 300;
pub const DEFAULT_PLANNER_SAMPLE_LIMIT: i64 = 10;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClientPressureTarget {
    pub client_name: String,
    pub connection_uri: Option<String>,
}

#[derive(Debug, Clone)]
pub struct ClientPressureWorkerConfig {
    pub logging_client_name: String,
    pub worker_interval_secs: u64,
    pub retention_days: i64,
    pub load_interval_secs: i64,
    pub planner_sample_limit: i64,
}

#[async_trait]
pub trait ClientPressureRuntime: Send + Sync {
    async fn open_logging_pool(&self, logging_client_name: &str) -> Result<PgPool>;

    async fn load_client_targets(
        &self,
        logging_pool: &PgPool,
    ) -> Result<HashMap<String, ClientPressureTarget>>;

    async fn explain_max_total_cost(
        &self,
        client_name: &str,
        target: &ClientPressureTarget,
        queries: &[String],
    ) -> Result<Option<f64>>;
}
