<?php

namespace Model;

use Entity\quizEntity;
use Gateway\QuizGateway;

class QuizModel {

    private QuizGateway $gateway;

    public function __construct(QuizGateway $gw)
    {
        $this -> gateway = $gw;
    }

    public function createQuiz(int $id_quiz, int $nb_questions) : bool
    {
        return $this -> gateway -> create($id_quiz, $nb_questions);
    }

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

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

    public function getAllQuiz() : array
    {
        $q = $this -> gateway -> findAll();

        $quizzes = [];

        foreach ($q as $quiz) {
            $quizzes[] = new quizEntity(
                $quiz['id_quiz'],
                $quiz['nb_questions']
            );
        }
        return $quizzes;

    }

}