mod hello;
mod home;
mod items;
mod shelves;

use atelier::router::methods::*;
use std::pin::Pin;

use crate::prelude::*;

fn make_handler<Fut, F, R>(f: F) -> Handler
where
	Fut: Future<Output = R> + Send + 'static,
	F: FnMut(Request, RequestCtx) -> Fut + Clone + Send + Sync + 'static,
	R: IntoResponse,
{
	Box::new(move |req, ctx| {
		let mut f = f.clone();
		Box::pin(async move { f(req, ctx).await.into_response() })
	})
}

pub fn make_router() -> Router {
	let mut router = atelier::router::Router::new();

	macro_rules! r {
		($m:expr, $p:expr, $h:expr) => {
			router.register($m, $p, make_handler($h))
		};
	}

	r!(GET, "/", home::view);
	r!(GET, "/shelves", shelves::view);
	r!(GET, "/shelves/:shelf_xid", shelves::view_one);
	r!(GET, "/items", items::view);
	r!(GET, "/items/create", items::view_create);
	r!(GET, "/items/:item_xid", items::view_one);
	r!(POST, "/items/create", items::post_create);
	r!(GET, "/hello/:name", hello::view);

	r!(GET, "/static/*", serve_static);

	router
}

pub async fn serve_static(req: Request, ctx: RequestCtx) -> Response {
	let url = req.uri().path();

	"".into_response()
}
