<?php

namespace api\timeline;

use PDO;

require_once __DIR__ . "/../utils/config.php";

/**
 * TimelineData class
 * Define all functions which either get, update, create or delete timeline data
 */
class timelineData
{
    /**
     * Get all education data
     * @return array - Array of all education data or error message
     */
    public function getEduData(): array
    {
        $conn = dbConn();
        $stmt = $conn->prepare("SELECT ID, startPeriod, endPeriod, grade, course  FROM edu ORDER BY startPeriod DESC;");
        $stmt->execute();

        // set the resulting array to associative
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

        if ($result)
        {
            return $result;
        }
        return array("errorMessage" => "Error, edu data not found");
    }
    
    /**
     * Get all work data
     * @return array - Array of all work data or error message
     */
    public function getWorkData(): array
    {
        $conn = dbConn();
        $stmt = $conn->prepare("SELECT ID, startPeriod, endPeriod, companyName, area, title  FROM work ORDER BY work.startPeriod DESC;");
        $stmt->execute();

        // set the resulting array to associative
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

        if ($result)
        {
            return $result;
        }
        return array("errorMessage" => "Error, work data not found");
    }

    /**
     * Update education data
     * @param string $dateFrom - Start date
     * @param string $dateTo - End date
     * @param string $grade - Grade
     * @param string $course - Course
     * @param string $ID - ID of the education data
     * @return string - "not found" if the ID is not found, "ok" if successful, "error" if not
     */
    public function updateEduData(string $dateFrom, string $dateTo, string $grade, string $course, string $ID): string
    {
        $conn = dbConn();
        $chkStmt = $conn->prepare("SELECT ID FROM edu WHERE ID = :id;");
        $chkStmt->bindParam(":id", $ID);
        $chkStmt->execute();
        $result = $chkStmt->fetch(PDO::FETCH_ASSOC);
        if (!$result)
        {
            return "not found";
        }

        $stmt = $conn->prepare("UPDATE edu SET startPeriod = :dateFrom, endPeriod = :dateTo, grade = :grade, course = :course WHERE ID = :id;");
        $stmt->bindParam(":dateFrom", $dateFrom);
        $stmt->bindParam(":dateTo", $dateTo);
        $stmt->bindParam(":grade", $grade);
        $stmt->bindParam(":course", $course);
        $stmt->bindParam(":id", $ID);
        if ($stmt->execute())
        {
            return "ok";
        }

        return "error";
    }

    /**
     * Update work data
     * @param string $dateFrom - Start date
     * @param string $dateTo - End date
     * @param string $companyName - Company name
     * @param string $area - Area
     * @param string $title - Title
     * @param string $ID - ID of the work data
     * @return string - "not found" if the ID is not found, "ok" if successful, "error" if not
     */
    public function updateWorkData(string $dateFrom, string $dateTo, string $companyName, string $area, string $title, string $ID): string
    {
        $conn = dbConn();
        $chkStmt = $conn->prepare("SELECT ID FROM work WHERE ID = :id;");
        $chkStmt->bindParam(":id", $ID);
        $chkStmt->execute();
        $result = $chkStmt->fetch(PDO::FETCH_ASSOC);
        if (!$result)
        {
            return "not found";
        }

        $conn = dbConn();
        $stmt = $conn->prepare("UPDATE work SET startPeriod = :dateFrom, endPeriod = :dateTo, companyName = :companyName, area = :area, title = :title WHERE ID = :id;");
        $stmt->bindParam(":dateFrom", $dateFrom);
        $stmt->bindParam(":dateTo", $dateTo);
        $stmt->bindParam(":companyName", $companyName);
        $stmt->bindParam(":area", $area);
        $stmt->bindParam(":title", $title);
        $stmt->bindParam(":id", $ID);
        if ($stmt->execute())
        {
            return "ok";
        }

        return "error";
    }

    /**
     * Delete education data by ID
     * @param int $ID
     * @return string - "not found" if the ID is not found, "ok" if successful, "error" if not
     */
    public function deleteEduData(int $ID): string
    {
        $conn = dbConn();
        $chkStmt = $conn->prepare("SELECT ID FROM edu WHERE ID = :id;");
        $chkStmt->bindParam(":id", $ID);
        $chkStmt->execute();
        $result = $chkStmt->fetch(PDO::FETCH_ASSOC);
        if (!$result)
        {
            return "not found";
        }

        $stmt = $conn->prepare("DELETE FROM edu WHERE ID = :id;");
        $stmt->bindParam(":id", $ID);
        if ($stmt->execute())
        {
            return "ok";
        }

        return "error";
    }

    /**
     * Delete work data by ID
     * @param int $ID
     * @return string - "not found" if the ID is not found, "ok" if successful, "error" if not
     */
    function deleteWorkData(int $ID): string
    {
        $conn = dbConn();
        $chkStmt = $conn->prepare("SELECT ID FROM work WHERE ID = :id;");
        $chkStmt->bindParam(":id", $ID);
        $chkStmt->execute();
        $result = $chkStmt->fetch(PDO::FETCH_ASSOC);
        if (!$result)
        {
            return "not found";
        }

        $stmt = $conn->prepare("DELETE FROM work WHERE ID = :id;");
        $stmt->bindParam(":id", $ID);
        if ($stmt->execute())
        {
            return "ok";
        }

        return "error";
    }
    
    /**
     * Create new education data
     * @param string $dateFrom - Start date
     * @param string $dateTo - End date
     * @param string $grade - Grade
     * @param string $course - Course
     * @return bool|int - ID of the new education data or false if not successful
     */
    public function addEduData(string $dateFrom, string $dateTo, string $grade, string $course): bool|int
    {
        $conn = dbConn();
        $stmt = $conn->prepare("INSERT INTO edu (startPeriod, endPeriod, grade, course) VALUES (:dateFrom, :dateTo, :grade, :course);");
        $stmt->bindParam(":dateFrom", $dateFrom);
        $stmt->bindParam(":dateTo", $dateTo);
        $stmt->bindParam(":grade", $grade);
        $stmt->bindParam(":course", $course);

        if ($stmt->execute())
        {
            return $conn->lastInsertId();
        }
        return false;
    }
    
    /**
     * Create new work data
     * @param string $dateFrom - Start date
     * @param string $dateTo - End date
     * @param string $companyName - Company name
     * @param string $area - Area
     * @param string $title - Title
     * @return bool|int - ID of the new work data if successful, false if not
     */
    public function addWorkData(string $dateFrom, string $dateTo, string $companyName, string $area, string $title): bool|int
    {
        $conn = dbConn();
        $stmt = $conn->prepare("INSERT INTO work (startPeriod, endPeriod, companyName, area, title) VALUES (:dateFrom, :dateTo, :companyName, :area, :title);");
        $stmt->bindParam(":dateFrom", $dateFrom);
        $stmt->bindParam(":dateTo", $dateTo);
        $stmt->bindParam(":companyName", $companyName);
        $stmt->bindParam(":area", $area);
        $stmt->bindParam(":title", $title);

        if ($stmt->execute())
        {
            return $conn->lastInsertId();
        }
        return false;
    }

}
