//! {{RESOURCE_NAME_PASCAL}} logic
//!
//! Generated by: keg gen api {{RESOURCE_NAME}}
//!
//! Pure business logic. No HTTP, no actix-web dependencies.

use crate::{{{PROJECT_NAME_SNAKE}}}::internal::{
    logic::{{RESOURCE_NAME}}_logic::{{RESOURCE_NAME_PASCAL}}Logic,
    model::entity::{{RESOURCE_NAME_PASCAL}},
    repository::{{RESOURCE_NAME}}_repo::{{RESOURCE_NAME_PASCAL}}Repository,
};
use anyhow::{Context, Result};
use uuid::Uuid;

/// Logic for {{RESOURCE_NAME_PASCAL}} operations
pub struct {{RESOURCE_NAME_PASCAL}}Logic {
    repo: {{RESOURCE_NAME_PASCAL}}Repository,
}

impl {{RESOURCE_NAME_PASCAL}}Logic {
    pub fn new(repo: {{RESOURCE_NAME_PASCAL}}Repository) -> Self {
        Self { repo }
    }

    /// List all {{RESOURCE_NAME_PASCAL}} entities
    pub async fn list(&self, limit: i64, offset: i64) -> Result<Vec<{{RESOURCE_NAME_PASCAL}}>> {
        self.repo.list(limit, offset).await
    }

    /// Get a single {{RESOURCE_NAME_PASCAL}} by ID
    pub async fn get(&self, id: Uuid) -> Result<Option<{{RESOURCE_NAME_PASCAL}}>> {
        self.repo.find_by_id(id).await
    }

    /// Create a new {{RESOURCE_NAME_PASCAL}}
    pub async fn create(&self, name: String) -> Result<{{RESOURCE_NAME_PASCAL}}> {
        let entity = {{RESOURCE_NAME_PASCAL}}::new(name);
        self.repo.insert(&entity).await?;
        Ok(entity)
    }

    /// Update an existing {{RESOURCE_NAME_PASCAL}}
    pub async fn update(&self, id: Uuid, name: Option<String>) -> Result<{{RESOURCE_NAME_PASCAL}}> {
        let mut entity = self
            .repo
            .find_by_id(id)
            .await?
            .context("entity not found")?;

        if let Some(name) = name {
            entity.name = name;
        }
        entity.updated_at = chrono::Utc::now();

        self.repo.update(&entity).await?;
        Ok(entity)
    }

    /// Delete a {{RESOURCE_NAME_PASCAL}} by ID
    pub async fn delete(&self, id: Uuid) -> Result<()> {
        self.repo.delete(id).await
    }
}