<?php
namespace Gateway;
use Connection;

Class UserGateway{
    private Connection $con;

    public function __construct(Connection $con){
        $this->con=$con;
    }

    public function insert(string $username,string $email,string $passwd):string{

        // récupération id
        $query='SELECT id_user FROM Users WHERE id_user >= ALL (SELECT id_user FROM Users);';
        $this->con->executeQuery($query);
        $result=$this->con->getResults();
        foreach($result as $row){
            $id=$row['id_user'] + 1;
        }
        // insertion user 
        $query='INSERT INTO Users VALUES (:id,:username,:email,:passwd,CURRENT_DATE,false);';
        $this->con->executeQuery($query,array(':id' => array($id,PDO::PARAM_STR),':username' => array($u->username,PDO::PARAM_STR),':email' => array($u->email,PDO::PARAM_STR),':passwd' => array($u->passwd,PDO::PARAM_STR)));
        return $id;
    }
    
    public function delete(string $id) : bool{

        // supretion user 
        $query='DELETE FROM Users WHERE id_user = :id;';
        return $this->con->executeQuery($query,array(':id' => array($id,PDO::PARAM_STR)));
    }

    public function getFavorite(string $id):array{

        //obtention favoris d'un user
        $query='SELECT * FROM Quote WHERE id_quote IN (SELECT id_quote IN Favorite f JOIN User u ON u.id_user = f.user WHERE id_user = :id);';
        $this->con->executeQuery($query,array(':id' => array($id,PDO::PARAM_STR)));
        $result=$this->con->getResults();
        return $result;
    }


    //obtenir les information d'un user
    public function findDataUser(int $id):array{
        $query = 'SELECT * FROM Users WHERE id_user=:idUser';
        $con->executeQuery($query, array(':idUser'=>array($id, PDO::PARAM_STR)));
        $result = $con->getResults();
        return $result;
    }

    // ===================== UPDATE FUNCTION =====================

    public function updateUsername(int $id, string $newUsername):array{
        //Update le nom du user passé en paramètre
        $queryUpdate = 'UPDATE Users SET username=:newUsername WHERE id_user=:idUser';
        $con->executeQuery($queryUpdate, array(':idUser'=>array($id, PDO::PARAM_STR), ':newUsername'=> array($newUsername, PDO::PARAM_STR)));

        //Renvoie le nouveau nom du user
        $queryReponse = 'SELECT username FROM Users WHERE id_user=:idUser';
        $con->executeQuery($queryReponse, array($id=>array($newUsername, PDO::PARAM_STR)));
        return $result;
    }

    public function updateEmail(int $id, string $newEmail):array{
        //Update le email du user passé en paramètre
        $queryUpdate = 'UPDATE Users SET email=:newEmail WHERE id_user=:idUser';
        $con->executeQuery($queryUpdate, array(':idUser'=>array($id, PDO::PARAM_STR), ':newEmail'=> array($newEmail, PDO::PARAM_STR)));

        //Renvoie le nouveau email du user
        $queryReponse = 'SELECT email FROM Users WHERE id_user=:idUser';    
        $con->executeQuery($queryReponse, array(':idUser'=>array($id, PDO::PARAM_STR)));
        $result = $con->getResults();
        return $result;
    }

    public function updateImg(int $id, int $newImg):array{
        //Update l'image du user passé en paramètre
        $query = 'UPDATE Users SET img=:newImg WHERE id_user=:idUser';
        $con->executeQuery($query, array(':idUser'=>array($id, PDO::PARAM_STR), ':newImg'=> array($newImg, PDO::PARAM_STR)));

        //Renvoie la nouvelle image du user
        $queryReponse = 'SELECT img FROM Users WHERE id_user=:idUser';
        $con->executeQuery($queryReponse, array(':idUser'=>array($id, PDO::PARAM_STR)));
        $result = $con->getResults();
        return $result;
    }

    public function updatePasswd(int $id, string $newPassWd):array{
        //Update le passwd du user passé en paramètre
        $query = 'UPDATE Users SET pssword=:newPassWd WHERE id_user=:idUser';
        $con->executeQuery($query, array(':idUser'=>array($id, PDO::PARAM_STR), ':newPassWd'=> array($newPassWd, PDO::PARAM_STR)));

        //Renvoie le nouveau passwd du user
        $queryReponse = 'SELECT pssword FROM Users WHERE id_user=:idUser';
        $con->executeQuery($queryReponse, array(':idUser'=>array($id, PDO::PARAM_STR)));
        $result = $con->getResults();
        return $result;
    }

}

$uG = new UserGateway(new Connection("pgsql:host=londres;dbname=dblebeaulato","lebeaulato",""));

?>
