//! `ListAllFacts` — operator-facing inspection use case (`smos facts`).
//!
//! Thin orchestration over [`FactRepository::list_all`]: validates the
//! optional memory_key filter and delegates. Exists as a use case (rather
//! than a direct port call from the CLI / HTTP layer) to preserve the
//! hexagonal invariant every other `/v1/cli/*` endpoint upholds: both the
//! CLI local branch and the HTTP handler invoke the SAME use-case struct,
//! so byte-equal stdout is guaranteed by construction.
//!
//! Retrieval and finalize do NOT share this path — they use the
//! status-specific `list_accepted` / `list_pending` contracts, which
//! carry stronger guarantees (retrieval pool is Accepted-only; finalize
//! input is Pending-only).

use smos_domain::{Fact, MemoryKey};

use crate::errors::UseCaseError;
use crate::ports::FactRepository;

/// Inspection-only listing of every fact in the store, optionally narrowed
/// to one memory namespace. Returns facts of ALL statuses
/// (Pending + Accepted + Rejected), ordered by `extracted_at DESC` when
/// backed by `SurrealStore`.
pub struct ListAllFacts<'a, FR> {
    pub facts: &'a FR,
}

impl<'a, FR: FactRepository> ListAllFacts<'a, FR> {
    /// Run the inspection query.
    ///
    /// `memory_key = None` is a cross-namespace scan; `Some(mk)` filters
    /// inside one namespace.
    pub async fn execute(&self, memory_key: Option<&MemoryKey>) -> Result<Vec<Fact>, UseCaseError> {
        Ok(self.facts.list_all(memory_key).await?)
    }
}
