use axum::Router;
use page::{PageServer, redirect::Redirect};
use std::sync::Arc;
use tracing::info;

use crate::pages;

pub mod assets;

pub(super) fn router() -> Router<()> {
	let (asset_prefix, asset_router) = assets::asset_router();
	info!("Serving assets at {asset_prefix}");

	let router = build_server().into_router();

	Router::new().merge(router).nest(asset_prefix, asset_router)
}

fn build_server() -> Arc<PageServer> {
	let server = PageServer::new();

	#[expect(clippy::unwrap_used)]
	server
		.add_page("/", pages::index())
		.add_page("/links", pages::links())
		.add_page("/whats-a-betalupi", pages::betalupi())
		.add_page("/handouts", pages::handouts())
		.add_page("/htwah", Redirect::new("/handouts").unwrap())
		.add_page("/htwah/typesetting", pages::htwah_typesetting());

	server
}

#[test]
fn server_builds_without_panic() {
	tokio::runtime::Builder::new_current_thread()
		.enable_all()
		.build()
		.unwrap()
		.block_on(async {
			// Needs tokio context
			let _server = build_server().into_router();
		});
}
