<?php

namespace api\blog;
require_once __DIR__ . "/../utils/routesInterface.php";
require_once "blogData.php";

use api\utils\routesInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\App;

class blogRoutes implements routesInterface
{
    private blogData $blogData;
    /**
     * constructor used to instantiate a base blog routes, to be used in the index.php file.
     * @param App $app - the slim app used to create the routes
     */
    public function __construct(App $app)
    {
        $this->blogData = new blogData();
        $this->createRoutes($app);
    }

    /**
     * creates the routes for the blog
     * @param App $app - the slim app used to create the routes
     * @return void - returns nothing
     */
    public function createRoutes(App $app): void
    {
        $app->get("/blog/post", function (Request $request, Response $response)
        {
            $posts = $this->blogData->getBlogPosts();

            $json = json_encode($posts);

            $response->getBody()->write($json);

            if (array_key_exists("errorMessage", $posts))
            {
                $response->withStatus(404);
            }

            return $response;
        });

        $app->get("/blog/post/{id}", function (Request $request, Response $response, $args)
        {
            if ($args["id"] != null)
            {
                $post = $this->blogData->getBlogPost($args["id"]);
                if (array_key_exists("errorMessage", $post))
                {
                    $response->getBody()->write(json_encode($post));
                    return $response->withStatus(404);
                }

                $response->getBody()->write(json_encode($post));
                return $response;
            }

            $response->getBody()->write(json_encode(array("error" => "Please provide an ID")));
            return $response->withStatus(400);
        });

        $app->patch("/blog/post/{id}", function (Request $request, Response $response, $args)
        {
            $data = $request->getParsedBody();
            if ($args["id"] != null)
            {
                if (empty($data["title"]) || strlen($data["featured"]) == 0 || empty($data["body"]) || empty($data["dateModified"]) || empty($data["categories"]))
                {
                    // uh oh sent some empty data
                    $response->getBody()->write(json_encode(array("error" => "Only some of the data was sent")));
                    return $response->withStatus(400);
                }

                $message = $this->blogData->updatePost($args["id"], $data["title"], intval($data["featured"]), $data["body"], $data["dateModified"], $data["categories"]);

                if ($message === "post not found")
                {
                    // uh oh something went wrong
                    $response->getBody()->write(json_encode(array("error" => "Error, post not found")));
                    return $response->withStatus(404);
                }

                if ($message === "unset featured")
                {
                    // uh oh something went wrong
                    $response->getBody()->write(json_encode(array("error" => "Error, cannot unset featured post, try updating another post to be featured first")));
                    return $response->withStatus(409);
                }

                if (!is_bool($message) || $message === false)
                {
                    // uh oh something went wrong
                    $response->getBody()->write(json_encode(array("error" => $message)));
                    return $response->withStatus(500);
                }

                return $response;
            }

            $response->getBody()->write(json_encode(array("error" => "Please provide an ID")));
            return $response->withStatus(400);
        });

        $app->delete("/blog/post/{id}", function (Request $request, Response $response, $args)
        {
            if ($args["id"] != null)
            {
                $message = $this->blogData->deletePost($args["id"]);

                if ($message === "post not found")
                {
                    // uh oh something went wrong
                    $response->getBody()->write(json_encode(array("error" => "Error, post not found")));
                    return $response->withStatus(404);
                }

                if ($message === "error")
                {
                    // uh oh something went wrong
                    $response->getBody()->write(json_encode(array("error" => "Error, something went wrong")));
                    return $response->withStatus(500);
                }

                if ($message === "cannot delete")
                {
                    // uh oh something went wrong
                    $response->getBody()->write(json_encode(array("error" => "Error, cannot delete featured post")));
                    return $response->withStatus(409);
                }
                return $response;
            }

            $response->getBody()->write(json_encode(array("error" => "Please provide an ID")));
            return $response->withStatus(400);
        });

        $app->post("/blog/post", function (Request $request, Response $response)
        {
            $data = $request->getParsedBody();
            $files = $request->getUploadedFiles();
            $headerImg = $files["headerImg"];
            if (empty($data["title"]) || strlen($data["featured"]) == 0 || empty($data["body"]) || empty($data["dateCreated"]) || empty($data["categories"]))
            {
                // uh oh sent some empty data
                $response->getBody()->write(json_encode(array("error" => "Error, empty data sent")));
                return $response->withStatus(400);
            }

            if (empty($files["headerImg"]))
            {
                $headerImg = null;
            }

            $featured = $data["featured"] === "true";
            $insertedID = $this->blogData->createPost($data["title"], $data["body"], $data["dateCreated"], $featured, $data["categories"], $headerImg);
            if (!is_int($insertedID))
            {
                // uh oh something went wrong
                $response->getBody()->write(json_encode(array("error" => $insertedID)));
                return $response->withStatus(500);
            }
            $response->getBody()->write(json_encode(array("ID" => $insertedID)));
            return $response->withStatus(201);
        });

        $app->post("/blog/uploadPostImage", function (Request $request, Response $response)
        {
            $files = $request->getUploadedFiles();
            if (empty($files))
            {
                // uh oh sent some empty data
                $response->getBody()->write(json_encode(array("error" => array("message" => "Error, empty data sent"))));
                return $response->withStatus(400);
            }

            $message = $this->blogData->uploadPostImage($files["upload"]);
            if (!is_array($message))
            {
                $response->getBody()->write(json_encode(array("error" => array("message" => $message))));
                return $response->withStatus(500);
            }

            $response->getBody()->write(json_encode($message));
            return $response->withStatus(201);
        });

        $app->post("/blog/headerImage/{id}", function (Request $request, Response $response, $args)
        {
            $files = $request->getUploadedFiles();

            if ($args["id"] != null)
            {
                if (empty($files))
                {
                    // uh oh sent some empty data
                    $response->getBody()->write(json_encode(array("error" => array("message" => "Error, empty data sent"))));
                    return $response->withStatus(400);
                }

                $message = $this->blogData->uploadHeaderImage($args["id"], $files["headerImg"]);
                if (!is_array($message))
                {
                    $response->getBody()->write(json_encode(array("error" => array("message" => $message))));
                    return $response->withStatus(500);
                }

                $response->getBody()->write(json_encode($message));
                return $response->withStatus(201);
            }

            $response->getBody()->write(json_encode(array("error" => "Please provide an ID")));
            return $response->withStatus(400);
        });
    }
}