<?php

namespace Gateway;
use Gateway\Connection;
use PDO;


class QuizGateway extends Gateway
{

    public function create(int $id_quiz, int $nb_questions, string $id_img, string $title) : bool
    {
        $query = "
            INSERT INTO Quiz
            VALUES (:id_q, :nb_q, :id_img, :title)
        ";

        return $this -> co -> executeQuery($query, [
            ':id_q' => array($id_quiz, PDO::PARAM_INT),
            ':nb_q' => array($nb_questions, PDO::PARAM_INT),
            ':id_img' => array($id_img, PDO::PARAM_STR),
            ':title' => array($title, PDO::PARAM_STR)
        ]);
    }

    public function findQuizById(int $id) : array
    {
        $query = "SELECT id_quiz, title, imgPath, nb_quest FROM Quiz q JOIN Image i on q.img = i.id_img WHERE id_quiz = :id_q"; 
        $this -> co -> executeQuery($query, [':id_q' => array($id, PDO::PARAM_INT)]);
        return $this -> co -> getResults();
    }

    public function delete(int $id) : bool
    {
        $query = "DELETE FROM Quiz WHERE id_quiz = :id_q";
        return $this -> co -> executeQuery($query, [':id_q' => array($id, PDO::PARAM_INT)]);
    }

    public function findAll() : array
    {
        $query = "SELECT id_quiz, title, imgPath, nb_quest FROM Quiz q JOIN Image i on q.img = i.id_img"; 
        $this -> co -> executeQuery($query);
        return $this -> co -> getResults();
    }

    public function getNumberOfQuestion(int $id) : intdiv{
        $query = "SELECT nb_quest FROM quiz WHERE id_quiz = :id_q";
        $this -> co -> executeQuery($query, [':id_q' => array($id, PDO::PARAM_INT)]);
        return $this -> co -> getResults();
    }

}