//! Typesense-facing sync, search-proxy, and worker-domain primitives for Athena.
//!
//! This crate owns the reusable logic behind Athena's optional Typesense
//! integration while keeping app-specific auth and Actix route wiring in the
//! server crate.

mod service;

use reqwest::Client;
use sqlx::PgPool;

pub use service::{
    TypesenseConnectionRequest, TypesenseSearchRequest, cancel_typesense_sync_job,
    ensure_typesense_sync_runs_schema_compat_once_for_pool, kill_typesense_sync_run,
    list_typesense_collections, run_due_sync_jobs_once, run_typesense_sync_job, search_typesense,
    test_typesense_backend,
};

/// Narrow runtime contract needed by the reusable Typesense domain logic.
pub trait TypesenseRuntime: Send + Sync {
    /// Shared reqwest client used for outbound Typesense requests.
    fn http_client(&self) -> &Client;

    /// Logging/catalog pool used to load sync jobs and write run state.
    fn logging_pool(&self) -> Option<PgPool>;

    /// Live source-client Postgres pool lookup by logical client name.
    fn postgres_pool_for_client(&self, client_name: &str) -> Option<PgPool>;

    /// Whether plain HTTP Typesense backends are allowed.
    fn typesense_allow_http(&self) -> bool;

    /// Maximum retry attempts for import/backup/rollback flows.
    fn typesense_import_max_attempts(&self) -> usize;

    /// Base retry delay for bounded backoff.
    fn typesense_import_retry_base_ms(&self) -> u64;

    /// Whether saga backup + compensation is enabled.
    fn typesense_sync_saga_backup_enabled(&self) -> bool;
}
