#pragma once
//stl
#include <iostream>
#include <memory>
#include <filesystem>
//robotics
#include <robotics/logger.hpp>
//local
#include "../response/response.hpp"
/// @brief before
struct http_before {
	/**
	 * @brief true控制器 false文件
	 * @param controller_action 
	 * @param request 
	 * @param response 
	 * @return 
	 */
	bool before(std::shared_ptr<v3::mvc::controller_action> controller_action, http::web_request& request, http::web_response& response) {
		if (request.path() == "/") {
			throw default_page_response();
		}
		if (!controller_action) {
			std::filesystem::path root_directory = v3::mvc::http_context::current()->root_directory;
			std::filesystem::path route = std::string(request.path().begin(), request.path().end());
			root_directory.make_preferred();
			root_directory /= route.make_preferred().relative_path();
			if (!std::filesystem::exists(root_directory)) {
				throw not_found_response();
			}
		}
		else {
			if (controller_action->authen && !v3::mvc::http_context::current()->authen()) {
				if (!controller_action->authen_jump.empty()) {
					throw v3::mvc::jump_response(controller_action->authen_jump);
				}
				throw default_page_response();
			}
			else if (v3::mvc::http_context::current()->authen()) {
				if (!controller_action->authen_jump.empty()) {
					throw v3::mvc::jump_response(controller_action->authen_jump);
				}
			}
		}
		return (bool)controller_action;
	}
};
/**
 * @brief 错误
 */
struct http_error {
	void error(std::string const& err, http::web_request& request, http::web_response& response) {
		robotics::v3::mvc::web_base_response().fill_page("internal server error.", http::status::internal_server_error);
		LOG_ERROR << err;
	}
};
/**
 * @brief 
 */
struct http_after {
	void after(http::web_request& request, http::web_response& response) {

	}
};