pub mod entity;
pub mod prelude;

#[derive(Clone, Debug, bon::Builder)]
pub struct TaskService<TR> {
    pub task_repository: TR,
}

impl<TR> prelude::TaskService for TaskService<TR>
where
    TR: prelude::TaskRepository,
{
    async fn find_by_target_ids(
        &self,
        method: entity::TaskMethod,
        target_ids: &[u64],
    ) -> anyhow::Result<Vec<self::entity::Task>> {
        self.task_repository
            .find_by_target_ids(method, target_ids)
            .await
    }

    async fn start_task(&self) -> anyhow::Result<Option<self::entity::Task>> {
        self.task_repository.start_task().await
    }

    async fn fail_task(&self, task_id: u64, error: anyhow::Error) -> anyhow::Result<()> {
        self.task_repository
            .fail_task(task_id, error.to_string())
            .await
    }

    async fn complete_task(&self, task_id: u64) -> anyhow::Result<()> {
        self.task_repository.complete_task(task_id).await
    }
}
