use axum::extract::State;
use axum::response::Html;
use axum::routing::get;
use axum::Router;
use minijinja::context;

use crate::state::AppState;

pub fn router() -> Router<AppState> {
    Router::new()
        .route("/", get(landing))
        .route("/pricing", get(pricing))
        .route("/components", get(components))
}

async fn landing(State(state): State<AppState>) -> Result<Html<String>, axum::http::StatusCode> {
    let html = state
        .templates
        .render("pages/landing.html.jinja", context! {
            title => "Forage - The Platform for Forest",
            description => "Push a forest.cue manifest, get production infrastructure.",
        })
        .map_err(|e| {
            tracing::error!("template error: {e:#}");
            axum::http::StatusCode::INTERNAL_SERVER_ERROR
        })?;

    Ok(Html(html))
}

async fn pricing(State(state): State<AppState>) -> Result<Html<String>, axum::http::StatusCode> {
    let html = state
        .templates
        .render("pages/pricing.html.jinja", context! {
            title => "Pricing - Forage",
            description => "Simple, transparent pricing. Pay only for what you use.",
        })
        .map_err(|e| {
            tracing::error!("template error: {e:#}");
            axum::http::StatusCode::INTERNAL_SERVER_ERROR
        })?;

    Ok(Html(html))
}

async fn components(State(state): State<AppState>) -> Result<Html<String>, axum::http::StatusCode> {
    let html = state
        .templates
        .render("pages/components.html.jinja", context! {
            title => "Components - Forage",
            description => "Discover and share reusable forest components.",
        })
        .map_err(|e| {
            tracing::error!("template error: {e:#}");
            axum::http::StatusCode::INTERNAL_SERVER_ERROR
        })?;

    Ok(Html(html))
}
