<?php

namespace Modules\API\Authenticate\Repository;

final class CredentialsRepository
{

    /**
     * @var \PDO $pdo
     */
    protected $pdo;

    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }
    
    /**
     * getInfoUser
     *
     * Voir pour la requete finale pour l'instant valeur de test
     *
     * @param  string $clientID
     * @param  string $clientSecret
     * @return array<mixed>|false
     */
    public function getInfoUser(string $clientID, string $clientSecret)
    {
        $req = "SELECT count(LIB01PARV) as nbrTuple
            FROM PARAVRAC with(nolock)
            WHERE (TYPEPARV = 'AUTHAPIOXA'
                    AND CODEPARV ='CLIENT_ID'
                    AND LIB01PARV = :clientID
                )
                OR (TYPEPARV = 'AUTHAPIOXA'
                    AND CODEPARV ='SECRET_ID'
                    AND LIB01PARV = :clientSecret
                )";
                
        
        
        $stmt = $this->pdo->prepare($req);

        $stmt->bindParam(':clientID', $clientID, \PDO::PARAM_STR);
        $stmt->bindParam(':clientSecret', $clientSecret, \PDO::PARAM_STR);

        $stmt->execute();

        return $stmt->fetch(\PDO::FETCH_ASSOC);
    }
    
    /**
     * getSecretID
     *
     * @return string
     */
    public function getSecretID()
    {
        $req = "SELECT LIB01PARV
            FROM PARAVRAC
            WHERE TYPEPARV = 'AUTHAPIOXA'
                    AND CODEPARV ='SECRET_ID'
                ";
        
        $stmt = $this->pdo->prepare($req);

        $stmt->execute();

        return $stmt->fetch(\PDO::FETCH_ASSOC)['LIB01PARV'] ?? '';
    }
}
