<?php

namespace Model;

use Entity\QuestionEntity;
use Entity\QuizEntity;
use Entity\QuizQuestionEntity;
use Gateway\Connection;
use Gateway\QuestionGateway;
use Gateway\QuizGateway;
use Gateway\QuizQuestionGateway;
use Gateway\Gateway;

class QuizQuestionModel extends Model
{

    public function createQuizQuestion(int $idQuiz, int $idQuestion): bool
    {
        return $this->gateway->create($idQuiz,$idQuestion);
    }

    public function findQuizQuestionByIdQuiz(int $id): ?QuizQuestionEntity
    {
        $q = $this ->gateway->findQuizQuestionByIdQuiz($id);
        if ($q) {
            return new QuizQuestionEntity(
                $q['idQuiz'],
                $q['idQuestion'],
            );
        }
        return null;
    }

    public function getAllQuestionByQuiz(int $id, Connection $co): array
    {
        $q = $this->gateway->findQuestionsFromQuiz($id);

        $questions = [];
        $gateway = new QuestionGateway($co);
        $qmdl = new QuestionModel($gateway);

        foreach ($q as $question){
            $questions [] = $qmdl->getQuestion($question[1]);
        }

        return $questions;
    }

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

        $quizzes = [];

        foreach ($q as $quiz) {
            $quizzes[] = new QuizQuestionEntity(
                $quiz['idQuiz'],
                $quiz['idQuestion']
            );
        }
        return $quizzes;
    }

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




}