<?php

namespace Model;
use Gateway\QuestionGateway;
use Entity\QuestionEntity;

class QuestionModel
{
    private QuestionGateway $gateway;

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

    public function createQuestion(int $id_question, string $question, string $answerA, string $answerB, string $answerC, string $answerD, string $cAnswer): bool
    {
        return $this -> gateway -> create($id_question, $question, $answerA, $answerB, $answerC, $answerD, $cAnswer);

    }

    public function getQuestion(int $id_question) : ?QuestionEntity
    {
        $q = $this -> gateway -> findById($id_question);
        if ($q)
            return new QuestionEntity(
                $q[0]['id_question'],
                $q[0]['texte'],
                $q[0]['answera'],
                $q[0]['answerb'],
                $q[0]['answerc'],
                $q[0]['answerd'],
                $q[0]['canswer']
            );
        return null;
    }

    public function updateTextQuestion(int $id_question, string $question) : bool
    {
        return $this -> gateway -> updateText($id_question, $question);
    }

    public function updateAnswersQuestion(int $id_question, string $answerA, string $answerB, string $answerC, string $answerD, string $cAnswer): bool
    {
        return $this -> gateway -> updateAnswers($id_question, $answerA, $answerB, $answerC, $answerD, $cAnswer);
    }

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

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

        $questions = [];

        foreach ($q as $question) {
            $questions[] = new QuestionEntity(
                $question['id_question'],
                $question['question'],
                $question['answerA'],
                $question['answerB'],
                $question['answerC'],
                $question['answerD'],
                $question['cAnswer']
            );
        }
        return $questions;
    }

    public function getRdmQuestion() : ?QuestionEntity
    {
        $q = $this -> gateway -> findRdmQuestion();
        if ($q)
            return new QuestionEntity(
                $q['id_question'],
                $q['question'],
                $q['answerA'],
                $q['answerB'],
                $q['answerC'],
                $q['answerD'],
                $q['cAnswer']
            );
        return null;
    }
}