use askama::Template;
use axum::{
    http::StatusCode,
    response::{Html, IntoResponse, Response},
};

use crate::database::{article::Article, link::Link};

/// A wrapper type that we'll use to encapsulate HTML parsed by askama into valid HTML for axum to serve.
pub struct HtmlTemplate<T>(pub T);

/// Allows us to convert Askama HTML templates into valid HTML for axum to serve in the response.
impl<T> IntoResponse for HtmlTemplate<T>
where
    T: Template,
{
    fn into_response(self) -> Response {
        // Attempt to render the template with askama
        match self.0.render() {
            // If we're able to successfully parse and aggregate the template, serve it
            Ok(html) => Html(html).into_response(),
            // If we're not, return an error or some bit of fallback HTML
            Err(err) => (
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Failed to render template. Error: {}", err),
            )
                .into_response(),
        }
    }
}

#[derive(Template)]
#[template(path = "article.html")]
pub struct ArticleTemplate {
    pub footer: String,
    pub content: String,
}

#[derive(Template)]
#[template(path = "page.html")]
pub struct PageTemplate {
    pub content: String,
}

#[derive(Template)]
#[template(path = "links.html")]
pub struct LinksPageTemplate {
    pub articles: Vec<Link>,
}

#[derive(Template)]
#[template(path = "blog.html")]
pub struct BlogTemplate {
    pub articles: Vec<Article>,
}

#[derive(Template)]
#[template(path = "now.html")]
pub struct NowTemplate {}

#[derive(Template)]
#[template(path = "garden.html")]
pub struct GardenTemplate {}

#[derive(Template)]
#[template(path = "books.html")]
pub struct BooksTemplate {}

#[derive(Template)]
#[template(path = "Tak.html")]
pub struct TakTemplate {}

#[derive(Template)]
#[template(path = "time.html")]
pub struct TimeTemplate {}

#[derive(Template)]
#[template(path = "uses.html")]
pub struct UsesTemplate {}

#[derive(Template)]
#[template(path = "gifts.html")]
pub struct GiftsTemplate {}

#[derive(Template)]
#[template(path = "contact.html")]
pub struct ContactTemplate {}

#[derive(Template)]
#[template(path = "ai.html")]
pub struct AiTemplate {}

#[derive(Template)]
#[template(path = "blogroll.html")]
pub struct BlogrollTemplate {
    pub blogs: Vec<Link>,
}

#[derive(Template)]
#[template(path = "EdgeDetection.html")]
pub struct EdgeDetectionTemplate {}

#[derive(Template)]
#[template(path = "ArchServer.html")]
pub struct ArchServerTemplate {}

#[derive(Template)]
#[template(path = "interests.html")]
pub struct InterestsTemplate {}

#[derive(Template)]
#[template(path = "home.html")]
pub struct HomeTemplate {}

#[derive(Template)]
#[template(path = "about.html")]
pub struct AboutTemplate {}

#[derive(Template)]
#[template(path = "blog_footer.html")]
pub struct BlogFooterTemplate {
    pub previous: String,
    pub next: String,
}
