<?php

namespace Modules\ProjetAchat\Repository;

class Acteur
{
    
    private $pdo;

    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }

    /**
     * @param array<string> $arrayAction
     * @return array<mixed>
    */
    public function countResponsableTaskFound(string $idObjet, string $oldReponsable, ?array $arrayAction = null, bool $search = true) : ?array
    {
        $req = "select COUNT(*) COUNT from DOA_TACHES with (nolock) where ID_OBJET = :idObjet and RESPONSABLE = :oldResponsable";
        
        if ($arrayAction) {
            $implode = implode("','", $arrayAction);
            $req .= " and ID_TACHE in ('$implode')";
        } elseif ($search) {
            $req .= " and ID_TACHE in ('')";
        }

        $arrayReq = [
            ':idObjet' => $idObjet,
            ':oldResponsable' => $oldReponsable,
        ];
        
        $requete = $this->pdo->prepare($req);
        $requete->execute($arrayReq);
        
        return $requete->fetch();
    }

    /**
     * @param array<string> $arrayAction
     * @return array<mixed>
    */
    public function countActeurActionFound(string $idObjet, string $oldReponsable, ?array $arrayAction = null, bool $search = true) : ?array
    {
        $req = "select COUNT(*) COUNT from DOA_ACTIONS_ACTEURS with (nolock) where ID_OBJET = :idObjet and CODE_ACTEUR = :oldResponsable";
        
        if ($arrayAction) {
            $implode = implode("','", $arrayAction);
            $req .= " and ID_ACTEUR in ('$implode')";
        } elseif ($search) {
            $req .= " and ID_ACTEUR in ('')";
        }

        $arrayReq = [
            ':idObjet' => $idObjet,
            ':oldResponsable' => $oldReponsable,
        ];
        
        $requete = $this->pdo->prepare($req);
        $requete->execute($arrayReq);

        return $requete->fetch();
    }

    /**
     * @param array<string> $listeTask
    */
    public function updateResponsableTask(string $idObjet, string $oldReponsable, string $newResponsable, array $listeTask) : void
    {
        $implode = implode("','", $listeTask);
        $req = "UPDATE DOA_TACHES set RESPONSABLE = :newResponsable where ID_OBJET =  :idObjet and RESPONSABLE = :oldResponsable and ID_TACHE in ('$implode')";

        $arrayReq = [
            ':idObjet' => (int)$idObjet,
            ':oldResponsable' => $oldReponsable,
            ':newResponsable' => $newResponsable
        ];
        
        $requete = $this->pdo->prepare($req);
        $requete->execute($arrayReq);
    }

    /**
     * @param array<string> $listeAction
    */
    public function updateActorAction(string $idObjet, string $oldReponsable, string $newResponsable, array $listeAction) : void
    {
        $implode = implode("','", $listeAction);
        $req = "UPDATE DOA_ACTIONS_ACTEURS set CODE_ACTEUR = :newResponsable, EMAIL = '' where ID_OBJET =  :idObjet and CODE_ACTEUR = :oldResponsable and ID_ACTEUR in ('$implode')";

        $arrayReq = [
            ':idObjet' => (int)$idObjet,
            ':oldResponsable' => $oldReponsable,
            ':newResponsable' => $newResponsable
        ];
        
        $requete = $this->pdo->prepare($req);
        $requete->execute($arrayReq);
    }
}
