<?php

namespace Model;

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

class QuizQuestionModel
{
    private QuizQuestionGateway $gw;

    /**
     * @param QuizGateway $gw
     */
    public function __construct(QuizQuestionGateway $gw)
    {
        $this->gw = $gw;
    }

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

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

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

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

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

        return $questions;
    }

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

        $quizzes = [];

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

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




}