use domain::{
    errors::DomainError,
    events::DomainEvent,
    models::{Person, PersonId},
};

use super::{deps::GetPersonDeps, should_enrich};

pub async fn execute(deps: &GetPersonDeps, id: PersonId) -> Result<Option<Person>, DomainError> {
    let person = deps.person_query.get_by_id(&id).await?;
    if let Some(ref p) = person
        && should_enrich(p)
    {
        let _ = deps
            .event_publisher
            .publish(&DomainEvent::PersonEnrichmentRequested {
                person_id: id,
                external_person_id: p.external_id().clone(),
            })
            .await;
    }
    Ok(person)
}

#[cfg(test)]
#[path = "tests/get.rs"]
mod tests;
