#include "../http.hpp"
#include "../tls_server.hpp"
#include "../router.hpp"
#include "../log.hpp"
#include "../queries/image.hpp"

void handle_pimage(HttpRequest& req, HttpResponse& resp, RouteContext& /*ctx*/) {
    std::string path = req.get_query_param("path");
    if (path.empty()) {
        resp.send_json(400, {{"Message", "Missing required query parameter 'path'."}});
        return;
    }

    std::string size = req.get_query_param("size", "200");
    ImageResult image = get_image_by_hash(path, size);
    if (image.buffer.empty()) {
        resp.send_json(404, {{"Message", "No image was found for path '" + path + "'."}});
        return;
    }

    resp.send_binary(200, image.buffer, image.content_type);
}
