use crate::html::{api, blog, projects, HtmlTemplate, NavBar};
use askama::Template;
use axum::{
    response::{IntoResponse, Redirect},
    routing::{get, Router},
    Extension,
};
use sqlx::PgPool;
use tower_http::services::ServeDir;

use super::{blog::get_articles_as_links_list, projects::get_projects_as_links_list};

#[derive(Clone)]
pub struct AppState {
    pub db: PgPool,
}

pub fn get_router(pool: PgPool) -> Router {
    let assets_path = std::env::current_dir().unwrap();
    let state = AppState { db: pool };
    Router::new()
        .nest("/api", api::get_router())
        .nest("/blog", blog::get_router())
        .nest("/projects", projects::get_router())
        .nest_service(
            "/assets",
            ServeDir::new(format!("{}/assets", assets_path.to_str().unwrap())),
        )
        .route("/", get(home))
        .route("/now", get(now))
        .route("/about", get(about))
        .route("/contact", get(contact))
        .route("/uses", get(uses))
        .route(
            "/robots.txt",
            get(|| async { Redirect::permanent("/assets/robots.txt") }),
        )
        .layer(Extension(state))
}

async fn home(state: Extension<AppState>) -> impl IntoResponse {
    let db_pool = &state.db;
    let article_list: Vec<String> = get_articles_as_links_list(db_pool)
        .await
        .expect("couldn't get articles");

    let (article_head, _) = article_list.split_at(5);

    let project_list: Vec<String> = get_projects_as_links_list(db_pool)
        .await
        .expect("Couldn't get projects");

    let (project_head, _) = project_list.split_at(5);

    let template = HomeTemplate {
        active_navbar: NavBar::HOME,
        recent_blogs: article_head.join("\n"),
        recent_projects: project_head.join("\n"),
    };
    HtmlTemplate(template)
}

#[derive(Template)]
#[template(path = "home.html")]
struct HomeTemplate {
    active_navbar: &'static str,
    recent_blogs: String,
    recent_projects: String,
}

async fn now() -> impl IntoResponse {
    let template = NowTemplate {
        active_navbar: NavBar::NOW,
    };
    HtmlTemplate(template)
}

#[derive(Template)]
#[template(path = "now.html")]
struct NowTemplate {
    active_navbar: &'static str,
}

async fn about() -> impl IntoResponse {
    let template = AboutTemplate {
        active_navbar: NavBar::ABOUT,
    };
    HtmlTemplate(template)
}

#[derive(Template)]
#[template(path = "about.html")]
struct AboutTemplate {
    active_navbar: &'static str,
}

async fn contact() -> impl IntoResponse {
    let template = ContactTemplate {
        active_navbar: NavBar::CONTACT,
    };
    HtmlTemplate(template)
}

#[derive(Template)]
#[template(path = "contact.html")]
struct ContactTemplate {
    active_navbar: &'static str,
}

async fn uses() -> impl IntoResponse {
    let template = UsesTemplate{
        active_navbar: NavBar::ABOUT,
    };
    HtmlTemplate(template)
}

#[derive(Template)]
#[template(path = "uses.html")]
struct UsesTemplate {
    active_navbar: &'static str,
}
