<?php
namespace Gateway;
use PDO;

Class UserGateway{

    private Connection $con;

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

    public function getNumberOfUsers() : array
    {
        $query = "SELECT Count(*) FROM Users";

        $this -> con -> executeQuery($query);

        return $this -> con -> getResults();
    }

    public function firstIdUser():int{
        $query = "Select id_user from Users;";
        $this -> con -> executeQuery($query);
        $res = $this -> con -> getResults();
        foreach($res as $r){
            $tab[] = $r["id_user"];
        }
        $id=1;
        while(in_array($id,$tab)){$id=$id+1;}
        return $id;
    }

    public function insertUser(string $pseudo, string $email, string $password, bool $isAdmin, int $imgPrfl) : bool {
        $id=$this->firstIdUser();
        $query = "
            INSERT INTO Users(id_user,username,email,password,creation,img)
            VALUES (:id, :pseudo, :email, :password, CURRENT_DATE, :imgPrfl);
        ";
        return $this -> con -> executeQuery($query, [
            ":id" => [$id, PDO::PARAM_INT],
            ":pseudo" => [$pseudo, PDO::PARAM_STR],
            ":email" => [$email, PDO::PARAM_STR],
            ":password" => [$password, PDO::PARAM_STR],
            ":imgPrfl" => [$imgPrfl, PDO::PARAM_STR]
        ]);
    }


    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 FROM Favorite f JOIN users u ON u.id_user = f.user_f WHERE u.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';
        $this->con->executeQuery($query, array(':idUser'=>array($id, PDO::PARAM_STR)));
        $result = $this->con->getResults();
        return $result;
    }

    // obtenir les informations d'un user selon son pseudo
    public function findUsername(string $username):array{
        $query = 'SELECT * FROM Users WHERE username= :username';
        $this->con->executeQuery($query, array(':username'=>array($username, PDO::PARAM_STR)));
        return $this->con->getResults();
    }

    public function findEmail(string $email):array{
        $query = 'SELECT * FROM Users WHERE email = :email';
        $this->con->executeQuery($query, array(':email'=>array($email, PDO::PARAM_STR)));
        return $this->con->getResults();
    }

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

    public function updateUsername(int $id, string $newUsername):bool{
        //Update le nom du user passé en paramètre
        $queryUpdate = 'UPDATE Users SET username=:newUsername WHERE id_user=:idUser';
        $this->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';

        return $this->con->executeQuery($queryReponse, array($id=>array($newUsername, PDO::PARAM_STR)));
    }

    public function updateEmail(int $id, string $newEmail):bool{
        //Update le email du user passé en paramètre
        $queryUpdate = 'UPDATE Users SET email=:newEmail WHERE id_user=:idUser';
        $this->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';
        return $this->con->executeQuery($queryReponse, array(':idUser'=>array($id, PDO::PARAM_STR)));
    }

    public function updateImg(int $id, int $newImg):array{
        //Update l'image du user passé en paramètre
        $query = 'UPDATE Users SET img_prfl=:newImg WHERE id_user=:idUser';
        $this->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_prfl FROM Users WHERE id_user=:idUser';
        $this->con->executeQuery($queryReponse, array(':idUser'=>array($id, PDO::PARAM_STR)));
        $result = $this->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';
        $this->con->executeQuery($query, array(':idUser'=>array($id, PDO::PARAM_STR), ':newPassWd'=> array($newPassWd, PDO::PARAM_STR)));
    }

    public function getIdUser(string $username):array{
        $query = 'SELECT id_user FROM Users WHERE username=:username';
        $this->con->executeQuery($query, array(':username'=>array($username, PDO::PARAM_STR)));
        $result = $this->con->getResults();
        return $result;
    }

}
?>
