<?php

namespace Model;

use Entity\QuizEntity;
use Gateway\QuizGateway;
use Gateway\Gateway;

class QuizModel  extends Model{
    
    public function createQuiz(int $id_quiz, int $nb_questions, String $id_img, string $title) : bool
    {
        return $this -> gateway -> create($id_quiz, $nb_questions, $id_img, $title);
    }

    public function getQuiz(int $id_quiz): ?QuizEntity
    {
        $q = $this -> gateway -> findQuizById($id_quiz);
        if ($q) {
            return new QuizEntity(
                $q[0]['id_quiz'],
                $q[0]['nb_quest'],
                $q[0]['imgpath'],
                $q[0]['title']
            );
        }
        return null;
    }

    public function deleteQuiz(int $id_quiz) : bool
    {
        return $this -> gateway -> delete($id_quiz);
    }

    public function listQuiz() : array
    {
        $res = $this -> gateway -> findAll();

        $quizs = [];

        foreach ($res as $quiz)
        {
            $quizs[] = new QuizEntity(
                $quiz['id_quiz'],
                $quiz['nb_quest'],
                $quiz['imgpath'],
                $quiz['title']                
            );
        }

        return $quizs;
    }
}