<?php
namespace Gateway;
use Gateway\Connection;
use PDO;

class QuestionGateway extends Gateway
{

    public function create(int $id_question, string $question, string $answerA, string $answerB, string $answerC, string $answerD, string $cAnswer): bool
    {
        $query = "
            INSERT INTO Question 
            VALUES (:id_q, :question, :answerA, :answerB, :answerC, :answerD, :cAnswer)
        ";

        return $this -> co -> executeQuery($query, [
            array('id_q' => $id_question, PDO::PARAM_INT),
            array('question' => $question, PDO::PARAM_STR),
            array('answerA' => $answerA, PDO::PARAM_STR),
            array('answerB' => $answerB, PDO::PARAM_STR),
            array('answerC' => $answerC, PDO::PARAM_STR),
            array('answerD' => $answerD, PDO::PARAM_STR),
            array('cAnswer' => $cAnswer, PDO::PARAM_STR),
        ]);
    }


    public function findById(int $id) : array
    {
        $query = "SELECT * FROM Question WHERE id_question = :id_q";
        $this -> co -> executeQuery($query, ['id_q' => array($id, PDO::PARAM_INT)]);
        return $this -> co -> getResults();
    }


    public function updateText(int $id_q, string $newQuestion) : bool
    {
        $query = "
            UPDATE Question
            SET question = :question
            WHERE id_question = :id_q
        ";

        return $this -> co -> executeQuery($query, [
            'id_q' => array($id_q, PDO::PARAM_INT),
            'question' => array($newQuestion, PDO::PARAM_STR)
        ]);
    }

    public function updateAnswers(int $id_q, string $newA, string $newB, string $newC, string $newD, string $newCorrect): bool
    {
        $query = "
            UPDATE Question
            SET answerA = :answerA, answerB = :answerB, 
                answerC = :answerC, answerD = :answerD, cAnswer = :cAnswer
            WHERE id_question = :id_q
        ";

        return $this -> co -> executeQuery($query, [
            'id_q' => array($id_q, PDO::PARAM_INT),
            'answerA' => array($newA, PDO::PARAM_STR),
            'answerB' => array($newB, PDO::PARAM_STR),
            'answerC' => array($newC, PDO::PARAM_STR),
            'answerD' => array($newD, PDO::PARAM_STR),
            'cAnswer' => array($newCorrect, PDO::PARAM_STR)
        ]);
    }

    /**
     * Deletes a question from the database by its ID
     *
     * @param int $id The ID of the question
     * @return bool
     */
    public function delete(int $id) : bool
    {
        $query = "DELETE FROM Question WHERE id_question = :id_q";

        return $this -> co -> executeQuery($query, ['id_q' => array($id, PDO::PARAM_INT)]);
    }

    /**
     * Retrieves all quizzes from the database
     *
     * @return QuestionEntity[] An array of `Question` objects
     */
    public function findAll() : array
    {
        $query = "SELECT * FROM Question";
        $this -> co -> executeQuery($query);
        return $this -> co -> getResults();
    }

    public function findRdmQuestion() : array
    {
        $query = "SELECT * FROM Question ORDER BY RANDOM() LIMIT 1";
        $this -> co -> executeQuery($query);
        return $this -> co -> getResults();
    }

    public function getQuestionQuoteSrc(string $l) : array{
        
    }

    
    /*--------------------------------------- Quiz Aléatoire ---------------------------------------*/

    /*----------------------- Question + Reponce Corecte -----------------------*/
    public function QuestionToReponceForQuoteToCarac(string $lang) : array
    {
        $query = "Select q.content as Question ,c.caracter as Reponce
                from Quote Q
                join Caracter c on c.id_caracter=q.id_caracter
                where q.langue=:lang and q.isValide = 'true'
                ORDER BY RANDOM()
                LIMIT 1;";
        $this -> co -> executeQuery($query, [':lang' => array($lang, PDO::PARAM_STR)]);
        return $this -> co -> getResults();
    }

    
    public function QuestionToReponceForQuoteToTitle(string $lang) : array
    {
        $query = "Select q.content as Question ,s.title as Reponce
                from Quote Q
                join Source s on s.id_source=q.id_source
                where q.langue=:lang and q.isValide = 'true'
                ORDER BY RANDOM()
                LIMIT 1;";
        $this -> co -> executeQuery($query, [':lang' => array($lang, PDO::PARAM_STR)]);
        return $this -> co -> getResults();
    }

    /*----------------------- Reponce Fausse -----------------------*/

    public function FakeReponceForQuoteToCarac(string $lang,string $corecteReponce) : array
    {
        $query = "Select c.caracter as Reponce
                from Quote Q
                join Caracter c on c.id_caracter=q.id_caracter
                where q.langue = :lang And c.caracter != :corecteReponce and q.isValide = 'true'
                ORDER BY RANDOM()
                LIMIT 3;";
        
        $this -> co -> executeQuery($query, [
            ':lang' => array($lang, PDO::PARAM_STR),
            ':corecteReponce' => array($lang, PDO::PARAM_STR)
        ]);
        return $this -> co -> getResults();
    }



    public function FakeReponceForQuoteToTitle(string $lang,string $corecteReponce) : array
    {
        $query = "Select s.title as Reponce
                from Quote Q
                join Source s on s.id_source=q.id_source
                where q.langue = :lang And s.title != :corecteReponce and q.isValide = 'true'
                ORDER BY RANDOM()
                LIMIT 3;";
        
        $this -> co -> executeQuery($query, [
            ':lang' => array($lang, PDO::PARAM_STR),
            ':corecteReponce' => array($lang, PDO::PARAM_STR)
        ]);
        return $this -> co -> getResults();
    }



}
