
#include "../cpp-httplib/httplib.h"
#include "files.hpp"

#include <fstream>
#include <iostream>
#include <string>
#include <format>

std::string data_index = Files::load("../static/index.html");
std::string data_projects = Files::load("../static/public/html/projects.html");
std::string data_contact = Files::load("../static/public/html/contact.html");
std::string data_about = Files::load("../static/public/html/about.html");
std::string data_resume = Files::load("../static/public/html/resume.html");
std::string data_404 = Files::load("../static/public/html/error.html");

httplib::Server svr;

auto page_handler(std::string data_content)
{
	return [data_content](const httplib::Request& req, httplib::Response& res)
	{
		std::string data = std::vformat(data_index, std::make_format_args(data_content));
		res.set_content(data, "text/html");
	};
}

void post_contact(const httplib::Request& req, httplib::Response& res)
{
	std::cout << "Contact form submitted\n";
	std::cout << "Name: " << req.get_param_value("name") << "\n";
	std::cout << "Email: " << req.get_param_value("email") << "\n";
	std::cout << "Message: " << req.get_param_value("message") << "\n";

	res.set_content("<p>Message sent. Thanks!</p>", "text/html");
}

void error_handler(const httplib::Request& req, httplib::Response& res)
{
	std::string data = std::vformat(data_index, std::make_format_args(data_404));
	res.set_content(data, "text/html");
}

int main()
{
	svr.set_mount_point("/static", "../static/public/");

	svr.Get("/", page_handler(data_projects));
	svr.Get("/projects", page_handler(data_projects));
	svr.Get("/contact", page_handler(data_contact));
	svr.Get("/about", page_handler(data_about));
	svr.Get("/resume", page_handler(data_resume));

	svr.Post("/contact", post_contact);

	svr.set_error_handler(error_handler);
	
	std::cout << "Listening on port 8080\n";
	svr.listen("0.0.0.0", 8080);
}

