<?php

namespace Modules\GlobalApplication\Repository;

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

class ArticlesAssocies extends AbsRepository
{
    /**
     * listeArticlesAssocies
     * @param string $code
     * @param string $type
     * @param array<string> $post
     * @param bool $export
     * @return array<mixed>
     */
    public function listeArticlesAssocies(string $code, string $type, array $post, bool $export = false) : ?array
    {
        if ($export) {
            $alias="ID_OBJ_ASS CODE_ARTICLES, CASE WHEN DES_ART IS NULL THEN DESIGN else DES_ART end ARTICLES, COEFF COEFFICIENT, OBLIG OBLIGATOIRE";
        } else {
            $alias="CODE_ART, CASE WHEN DES_ART IS NULL THEN DESIGN else DES_ART end DESIGN, COEFF, OBLIG";
        }
        
        $req = "SELECT ".$alias." FROM ART_ASSOC with (nolock) 
                LEFT JOIN ARTICLES with (nolock) ON ARTICLES.CODE_ART = ART_ASSOC.ID_OBJ_ASS
                left join TRAD_ART with(nolock) on TRAD_ART.COD_ART = ARTICLES.CODE_ART and COD_LNG = 'ENG'
                WHERE ART_ASSOC.T_ASS=:type AND ART_ASSOC.ID_OBJ= :value";
        
        $arrayReq = [
            ':value' => $code,
            ':type' => $type
        ];
        
        if (isset($post['OBLIG']) && $post['OBLIG']) {
            // @phpstan-ignore-next-line
            $post['OBLIG'] = Sql::toBool($post['OBLIG']);
        }
        
        $whereArray = \Helpers\Filter\FilterTable::splitFilter($post, 0, 0, 0);
        
        if (count($whereArray)>  0) {
            $req .= str_ireplace('where', 'and', $whereArray[0]).$whereArray['tri'];
            
            if (count($whereArray[1]) != 0) {
                $arrayReq=  array_merge($arrayReq, $whereArray[1]);
            }
        }

        return $this->fetchAssocAll($req, $arrayReq);
    }
    
    /**
     * suppresionArticles
     * @param string $code
     * @param string $type
     * @param string $codeArticles
     * @return void
     */
    public function suppresionArticles(string $code, string $type, string $codeArticles) : void
    {
        $req = "DELETE FROM ART_ASSOC WHERE T_ASS=:type and ID_OBJ=:value1 AND ID_OBJ_ASS=:value2";
        
        $arrayReq = [
            ':value1' => $code,
            ':value2' => $codeArticles,
            ':type' => $type
        ];
        
        $articles = $this->pdo->prepare($req);
        $articles->execute($arrayReq);

        return;
    }
    
    /**
     * nouveauArtcles
     * @param string $id
     * @param string $type
     * @param array<string> $post
     * @return void
     */
    public function nouveauArtcles(string $id, string $type, array $post) : void
    {
        $req = "INSERT INTO ART_ASSOC(T_ASS,ID_OBJ,T_OBJ_ASS,ID_OBJ_ASS,COEFF,OBLIG)
            VALUES(:type, :value1, 'A', :value2, ".$post['COEFF'].", ".sql::setBoolSql((int)$post['OBLIG']).")";
        
        $arrayReq = [
            ':value1' => $id,
            ':value2' => $post['ID_OBJ_ASS'],
            ':type' => $type,
        ];
        
        $articles = $this->pdo->prepare($req);
        $articles->execute($arrayReq);
    }
    
    /**
     * Return all the article associeted
     * @param string $code
     * @param string $type
     * @param string $codeArticles
     * @return array<string>
     */
    public function articles(string $code, string $type, string $codeArticles) : ?array
    {
        $req = "SELECT * FROM ART_ASSOC with (nolock) 
                LEFT JOIN ARTICLES with (nolock) ON ARTICLES.CODE_ART = ART_ASSOC.ID_OBJ_ASS
                WHERE T_ASS=:type AND ID_OBJ=:value1 AND ID_OBJ_ASS=:value2";
        
        $arrayReq = [
            ':value1' => $code,
            ':value2' => $codeArticles,
            ':type' => $type,
        ];
        
        return $this->fetchAssocOne($req, $arrayReq);
    }
    
    /**
     * updateArticles
     * @param string $code
     * @param string $type
     * @param string $codeArtcles
     * @param array<string> $post
     * @return void
     */
    public function updateArticles(string $code, string $type, string $codeArtcles, array $post) : void
    {
        $req = "UPDATE ART_ASSOC SET COEFF=".$post['COEFF'].", OBLIG=".sql::setBoolSql((int)$post['OBLIG'])." WHERE T_ASS=:type AND ID_OBJ=:value1 AND ID_OBJ_ASS=:value2";
        
        $arrayReq = [
            ':value1' => $code,
            ':value2' => $codeArtcles,
            ':type' => $type,
        ];
        
        $articles = $this->pdo->prepare($req);
        $articles->execute($arrayReq);
    }
}
