//! Template module for handling .tpl.toml files with Tera engine
//!
//! This module provides functionality to:
//! - Load and parse .tpl.toml configuration files
//! - Apply template values using Tera template engine
//! - Render final content from templates

pub mod config;
pub mod engine;
pub mod loader;
pub mod routes;
pub mod service;

use std::collections::HashMap;

pub use config::TemplateConfig;
pub use engine::TemplateEngine;
pub use loader::TemplateLoader;
use serde::{Deserialize, Serialize};
pub use service::TemplateService;

/// Template configuration loaded from .tpl.toml files
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemplatePageConfig {
    /// Name of the template file to use
    pub template_name: String,
    /// Values to be injected into the template
    pub values: HashMap<String, serde_json::Value>,
    /// Optional metadata
    pub metadata: Option<HashMap<String, serde_json::Value>>,
}

/// Rendered template result
#[derive(Debug, Clone)]
pub struct RenderedTemplate {
    /// The rendered HTML content
    pub content: String,
    /// Original template configuration
    pub config: TemplatePageConfig,
    /// Source file path
    pub source_path: String,
}

/// Template engine error types
#[derive(Debug, thiserror::Error)]
pub enum TemplateError {
    #[error("Template file not found: {0}")]
    #[allow(dead_code)]
    TemplateNotFound(String),

    #[error("Configuration file not found: {0}")]
    ConfigNotFound(String),

    #[error("Invalid template configuration: {0}")]
    InvalidConfig(String),

    #[error("Template parsing error: {0}")]
    ParseError(String),

    #[error("Template rendering error: {0}")]
    RenderError(String),

    #[error("IO error: {0}")]
    IoError(#[from] std::io::Error),

    #[error("TOML parsing error: {0}")]
    TomlError(#[from] toml::de::Error),

    #[error("Tera error: {0}")]
    TeraError(#[from] tera::Error),
}

pub type Result<T> = std::result::Result<T, TemplateError>;
