<?php
namespace api\timeline;
require_once __DIR__ . "/../utils/routesInterface.php";
require_once "timelineData.php";

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

class timelineRoutes implements routesInterface
{
    private timelineData $timelineData;

    /**
     * constructor used to instantiate a base timeline 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->timelineData = new timelineData();
        $this->createRoutes($app);
    }

    /**
     * creates the routes for the timeline
     * @param App $app - the slim app used to create the routes
     * @return void - returns nothing
     */
    public function createRoutes(App $app): void
    {
        $app->get("/timelineData/{timeline}", function (Request $request, Response $response, array $args)
        {
            //check if route is available if it is get the data
            //otherwise return an error
            if ($args["timeline"] == "edu")
            {
                $response->getBody()->write(json_encode($this->timelineData->getEduData()));
                return $response;
            }

            if($args["timeline"] == "work")
            {
                $response->getBody()->write(json_encode($this->timelineData->getWorkData()));
                return $response;
            }

            // something went wrong
            $response->getBody()->write(json_encode(array("errorMessage" => "Error, timeline data not found")));
            return $response->withStatus(404);
        });

        $app->patch("/timelineData/{timeline}/{id}", function (Request $request, Response $response, array $args)
        {
            $data = $request->getParsedBody();
            if ($args["timeline"] == "edu" && $args["id"] != null)
            {
                if (empty($data["dateFrom"]) || empty($data["dateTo"]) || empty($data["grade"]) || empty($data["course"]))
                {
                    // 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->timelineData->updateEduData($data["dateFrom"], $data["dateTo"], $data["grade"], $data["course"], $args["id"]);

                if ($message == "not found")
                {
                    // uh oh sent some empty data
                    $response->getBody()->write(json_encode(array("error" => "Edu data with ID " . $args["id"] . " was not found")));
                    return $response->withStatus(404);
                }

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

                $response->withStatus(201);
                return $response;
            }

            if ($args["timeline"] == "work" && $args["id"] != "undefined")
            {
                if (empty($data["dateFrom"]) || empty($data["dateTo"]) || empty($data["companyName"]) || empty($data["area"]) || empty($data["title"]))
                {
                    // 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->timelineData->updateWorkData($data["dateFrom"], $data["dateTo"], $data["companyName"], $data["area"], $data["title"], $args["id"]);

                if ($message == "not found")
                {
                    // uh oh sent some empty data
                    $response->getBody()->write(json_encode(array("error" => "Work data with ID " . $args["id"] . " was not found")));
                    return $response->withStatus(404);
                }

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

                $response->withStatus(201);
                return $response;
            }

            $response->getBody()->write(json_encode(array("error" => "The correct data was not sent")));
            return $response->withStatus(400);
        });

        $app->delete("/timelineData/{timeline}/{id}", function (Request $request, Response $response, array $args)
        {
            if ($args["timeline"] == "edu" && $args["id"] != null)
            {
                $message = $this->timelineData->deleteEduData($args["id"]);

                if ($message == "not found")
                {
                    // uh oh sent some empty data
                    $response->getBody()->write(json_encode(array("error" => "Edu data with ID " . $args["id"] . " was not found")));
                    return $response->withStatus(404);
                }

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

                return $response;
            }

            if ($args["timeline"] == "work" && $args["id"] != null)
            {
                $message = $this->timelineData->deleteWorkData($args["id"]);

                if ($message == "not found")
                {
                    // uh oh sent some empty data
                    $response->getBody()->write(json_encode(array("error" => "Work data with ID " . $args["id"] . " was not found")));
                    return $response->withStatus(404);
                }

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

                return $response;
            }

            $response->getBody()->write(json_encode(array("error" => "The correct data was not sent")));
            return $response->withStatus(400);
        });

        $app->post("/timelineData/{timeline}", function (Request $request, Response $response, array $args)
        {
            $data = $request->getParsedBody();
            if ($args["timeline"] == "edu")
            {
                if (empty($data["dateFrom"]) || empty($data["dateTo"]) || empty($data["grade"]) || empty($data["course"]))
                {
                    // uh oh sent some empty data
                    $response->getBody()->write(json_encode(array("error" => "Only some of the data was sent")));
                    return $response->withStatus(400);
                }

                $insertedID = $this->timelineData->addEduData($data["dateFrom"], $data["dateTo"], $data["grade"], $data["course"]);
                if (!is_int($insertedID))
                {
                    // uh oh something went wrong
                    $response->getBody()->write(json_encode(array("error" => "Something went wrong")));
                    return $response->withStatus(500);
                }

                $response->getBody()->write(json_encode(array("ID" => $insertedID)));
                $response->withStatus(201);
                return $response;
            }

            if ($args["timeline"] == "work")
            {
                if (empty($data["dateFrom"]) || empty($data["companyName"]) || empty($data["area"]) || empty($data["title"]))
                {
                    // uh oh sent some empty data
                    $response->getBody()->write(json_encode(array("error" => "Only some of the data was sent")));
                    return $response->withStatus(400);
                }

                if (empty($data["dateTo"]))
                {
                    $data["dateTo"] = "";
                }

                $insertedID = $this->timelineData->addWorkData($data["dateFrom"], $data["dateTo"], $data["companyName"], $data["area"], $data["title"]);
                if (!is_int($insertedID))
                {
                    // uh oh something went wrong
                    $response->getBody()->write(json_encode(array("error" => "Something went wrong")));
                    return $response->withStatus(500);
                }

                $response->getBody()->write(json_encode(array("ID" => $insertedID)));
                $response->withStatus(201);
                return $response;
            }

            $response->getBody()->write(json_encode(array("error" => "The correct data was not sent")));
            return $response->withStatus(400);
        });
    }
}