<?php

namespace Gateway;

use PDO;

class ResultsGateway extends Gateway
{

    public function createResultsGateway(int $idQuiz, int $idUser, int $nbPts, int $time) : bool
    {
        $query = "
            INSERT INTO Results
            VALUES (:id_quiz, :id_user, :nb_pts, :time)
        ";

        return $this -> co -> executeQuery($query, [
            'id_quiz' => array($idQuiz, PDO::PARAM_INT),
            'id_user' => array($idUser, PDO::PARAM_INT),
            'nb_pts' => array($nbPts, PDO::PARAM_INT),
            'time' => array($time, PDO::PARAM_INT)
        ]);
    }

    public function findResultsByQuiz(int $idQuiz) : array
    {
        $query = "
            SELECT * FROM Results
            WHERE quiz_r = :id_quiz
        ";

        $this -> co -> executeQuery($query, ['id_quiz' => array($idQuiz, PDO::PARAM_INT)]);

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

    public function findResultsByUser(int $idUser) : array
    {
        $query = "
            SELECT * FROM Results
            WHERE user_r = :id_user
        ";

        $this -> co -> executeQuery($query, ['id_user' => array($idUser, PDO::PARAM_INT)]);

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

    public function findResultsById(int $idQuiz, int $idUser) : array
    {
        $query = "
            SELECT * FROM Results
            WHERE quiz_r = :id_quiz AND user_r = :id_user
        ";

        $this -> co -> executeQuery($query, [
            'id_user' => array($idUser, PDO::PARAM_INT),
            'id_quiz' => array($idQuiz, PDO::PARAM_INT)
        ]);

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

    public function findAllResults() : array
    {
        $query = "SELECT * FROM Results";

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

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

    public function updateResults(int $idQuiz, int $idUser, ?int $score, ?int $time) : bool
    {
        if ($score && !$time)
        {
            $query = "
                UPDATE Results
                SET score = :score
                WHERE quiz_r = :id_quiz AND user_r = :id_user
            ";

            return $this -> co -> executeQuery($query, [
                'id_quiz' => array($idQuiz, PDO::PARAM_INT),
                'id_user' => array($idUser, PDO::PARAM_INT),
                'score' => array($score, PDO::PARAM_INT)
            ]);
        }


        else if (!$score && $time)
        {
            $query = "
                UPDATE Results
                SET time = :time
                WHERE quiz_r = :id_quiz AND user_r = :id_user
            ";

            return $this -> co -> executeQuery($query, [
                'id_quiz' => array($idQuiz, PDO::PARAM_INT),
                'id_user' => array($idUser, PDO::PARAM_INT),
                'time' => array($time, PDO::PARAM_INT)
            ]);
        }

        else
        {
            $query = "
                UPDATE Results
                SET score = :score AND time = :time
                WHERE quiz_r = :id_quiz AND user_r = :id_user
            ";

            return $this -> co -> executeQuery($query, [
                'id_quiz' => array($idQuiz, PDO::PARAM_INT),
                'id_user' => array($idUser, PDO::PARAM_INT),
                'score' => array($score, PDO::PARAM_INT),
                'time' => array($time, PDO::PARAM_INT)
            ]);
        }

    }


}