<?php

namespace App\Controller\Api;

use App\Entity\EmployeesEntity;
use App\Services\Response\JsonResponse;
use App\Table\EmployeesTable;

class EmployeController extends ApiController
{

    public function getAll()
    {
        //On récupère les données request
        $post = $this->getDatas();
        if (!empty($post)) {
            if (isset($post["api_key"]) && !empty($post["api_key"])) {
                $apiKey = $post["api_key"];
                //verification api_key
                $this->validateApiKey($apiKey);

                $date = new \DateTime();
                $date->sub(new \DateInterval("P10D"));
                /**
                 * @var $employeesTable EmployeesTable
                 */
                $employeesTable = $this->getKernel()->getModelByKey(EmployeesTable::class);
                $employes = $employeesTable->getAllActif($date->format("Y-m-d"));

                $this->response["employes"] = [];

                /**
                 * @var $employe EmployeesEntity
                 */
                foreach ($employes as $employe) {
                    $this->response["employes"][] = [
                        "reference" => $employe->getReference(),
                        "company_id" => $employe->getCompany(),
                        "first_name" => $employe->getFirstName(),
                        "last_name" => $employe->getLastName(),
                        "hiring_date" => $employe->getHiringDate(),
                        "leaving_date" => $employe->getLeavingDate(),
                        "keywords" => $employe->getLastName() . " " . $employe->getFirstName(),
                        "autocomplete_term" => $employe->getLastName() . " " . $employe->getFirstName(),
                    ];
                }

                if (!empty($this->response["employes"])) {
                    $this->response["success"] = true;
                    $this->response["message"] = "Voici la liste de tous les salariés";
                } else {
                    $this->response["success"] = false;
                    $this->response["message"] = "Une erreur est survenue lors de la récupération des salariés. Code erreur : api-all-salaries-3";
                }
            } else {
                $this->response["success"] = false;
                $this->response["message"] = "Votre requête est incomplète. Code erreur : api-all-salaries-2.";
            }
        } else {
            $this->response["success"] = false;
            $this->response["message"] = "Votre requête est incomplète. Code erreur : api-all-salaries-1.";
        }

        $jsonResponse = new JsonResponse($this->response);
        $jsonResponse->sendResponse();
    }

    /**
     * Route qui retourne tous les salariés actifs par compagnie Id
     * Retour sour forme de tableau
     */
    public function getAllByCompanyID()
    {
        //On récupère les données request
        $post = $this->getDatas();
        if (!empty($post)) {
            if (isset($post["company_id"], $post["api_key"]) &&
                !empty($post["api_key"]) &&
                !empty($post["company_id"])
            ) {
                $companyId = $post["company_id"];
                $apiKey = $post["api_key"];

                //verification api_key
                $this->validateApiKey($apiKey);

                if (!empty($companyId)) {
                    $date = new \DateTime();
                    $date->sub(new \DateInterval("P10D"));
                    /**
                     * @var $employeesTable EmployeesTable
                     */
                    $employeesTable = $this->getKernel()->getModelByKey(EmployeesTable::class);
                    $employes = $employeesTable->getAllByCompany($companyId, $date->format("Y-m-d"));

                    if (!empty($employes)) {
                        $datas = [];

                        /**
                         * @var $employe EmployeesEntity
                         */
                        foreach ($employes as $employe) {
                            $datas[] = [
                                "reference" => $employe->getReference(),
                                "company_id" => $employe->getCompany(),
                                "first_name" => $employe->getFirstName(),
                                "last_name" => $employe->getLastName(),
                                "hiring_date" => $employe->getHiringDate(),
                                "leaving_date" => $employe->getLeavingDate(),
                                "keywords" => $employe->getLastName() . " " . $employe->getFirstName(),
                                "autocomplete_term" => $employe->getLastName() . " " . $employe->getFirstName(),
                            ];
                        }

                        $this->response["employes"] = $datas;
                        $this->response["success"] = true;
                        $this->response["message"] = "Voici la liste des salariés pour la compagnie demandée";
                    } else {
                        $this->response["success"] = false;
                        $this->response["message"] = "Aucun résultat trouvé avec cette compagnie";
                    }
                } else {
                    $this->response["success"] = false;
                    $this->response["message"] = "Veuillez renseigner company_id";
                }
            } else {
                $this->response["success"] = false;
                $this->response["message"] = "Votre requête est incomplète. Code erreur : api-all-salaries-by-company-2.";
            }
        } else {
            $this->response["success"] = false;
            $this->response["message"] = "Votre requête est incomplète. Code erreur : api-all-salaries-by-company-1.";
        }

        $jsonResponse = new JsonResponse($this->response);
        $jsonResponse->sendResponse();
    }
}