<?php

namespace Modules\Parametrage\Incident\Repository;

use Helpers\QueryBuilder\AbsRepository;
use Helpers\Outil\Sql;

class ParamIncident extends AbsRepository
{
    /**
     * Get all incidents grouped by category
     * @param array<mixed> $post
     * @return array<mixed>
     */
    public function getAllIncidents($post): array
    {
        $param = [];
        $req = "SELECT 
                    COD_INC,
                    " . $this->selectTrad('LIBELLE', 'label') . ",
                    CODE_CAT,
                    GERE_HAP,
                    MASQUER
                FROM INCIDENT with (nolock)
                " . $this->joinTrad('LIBELLE', $param) . " WHERE 1=1
                ";


        $whereArray = \Helpers\Filter\FilterTable::splitFilter($post, 0, 0, 1);
        if (count($whereArray) >  0) {
            $req .= $whereArray[0] . $whereArray['tri'];
    
            if (count($whereArray[1]) != 0) {
                $param = array_merge($param, $whereArray[1]);
            }
        }
    
        $paginatorGestion = new \Helpers\Pagination\PaginatorGestion();
        $paginatorGestion->injecterSqlCount($req, $post, $param, $this->pdo);

        $results = $this->fetchAssocAll($req, $param);
        return $results;
    }

    /**
     * Get incidents by category
     * @param string $category
     * @return array<mixed>
     */
    public function getIncidentsByCategory(string $category): array
    {
        $param = [':category' => $category];
        $req = "SELECT 
                    COD_INC as code,
                    " . $this->selectTrad('LIBELLE', 'label') . ",
                    GERE_HAP as isManaged
                FROM INCIDENT with (nolock)
                " . $this->joinTrad('LIBELLE', $param) . "
                WHERE CODE_CAT = :category
                AND GERE_HAP = " . sql::setBoolSql(1) . "
                ORDER BY LIBELLE";
    
        return $this->fetchAssocAll($req, $param);
    }

    /**
     * @param array<mixed> $post
     * @return void
     */
    public function createIncident(array $post): void
    {
        $req = "INSERT INTO INCIDENT
                (COD_INC, LIBELLE, CODE_CAT, MASQUER, GERE_HAP)
                SELECT :code, :label, :category, :isHidden, " . sql::setBoolSql(0) . "
                FROM INCIDENT with (nolock)
                WHERE COD_INC = :parentCode AND GERE_HAP = " . sql::setBoolSql(1) . "
                HAVING COUNT(*) > 0";

        $params = [
            ':code' => $post['incident_code'],
            ':label' => $post['incident_label'],
            ':category' => $post['category_code'],
            ':isHidden' => sql::toBool($post['isHidden']),
            ':parentCode' => $post['parentCode']
        ];

        $incident = $this->pdo->prepare($req);
        $incident->execute($params);
    }

    /**
     * @param array<mixed> $data
     * @return void
     */
    public function updateIncident(array $data): void
    {
        $req = "UPDATE INCIDENT
                SET LIBELLE = :label, 
                    MASQUER = :isHidden
                WHERE COD_INC = :code AND GERE_HAP = " . sql::setBoolSql(1);

        $params = [
            ':code' => $data['incident_code'],
            ':label' => $data['new_libelle'],
            ':isHidden' => sql::toBool($data['isHidden']),
        ];

        $incident = $this->pdo->prepare($req);
        $incident->execute($params);
    }


    /**
     * Get all incident categories
     * @return array<mixed>
     */
    public function getIncidentCategories(): array
    {
        $param = [];
        $req = "SELECT DISTINCT CODE_CAT as category
                FROM INCIDENT with (nolock)
                ORDER BY CODE_CAT";

        return $this->fetchAssocAll($req, $param);
    }
}
