<?php
namespace Gateway;
use PDO;
Class QuoteGateway{
    private Connection $con;

    public function __construct(Connection $con){
        $this->con=$con;
    }

    public function searchQuote(string $quote,int $numpage,string $language):array{

        //recherche par citation
        $query="SELECT q.id_quote, q.content, c.caracter, c.img_path, s.title, s.date, q.like, q.language FROM Quote q JOIN Caracter c ON c.id_caracter = q.id_caracter JOIN Source s ON s.id_source = q.id_source WHERE content LIKE '%:quote%' AND isValid = true AND language = :language LIMIT 20 OFFSET :page*20;";
        $this->con->executeQuery($query,array(':quote' => array($quote,PDO::PARAM_STR),':page' => array($numpage,PDO::PARAM_INT),':language' => array($language,PDO::PARAM_STR)));
        $result=$this->con->getResults();
        return $result;
    }

    public function searchSource(string $source,int $numpage,string $language):array{

        //recherche par source
        $query="SELECT q.id_quote, q.content, c.caracter, c.img_path, s.title, s.date, q.like, q.language FROM Quote q JOIN Caracter c ON c.id_caracter = q.id_caracter JOIN Source s ON s.id_source = q.id_source  WHERE s.title LIKE '%:source%' AND q.isValid = true AND language = :language LIMIT 20 OFFSET :page*20;";
        $this->con->executeQuery($query,array(':source' => array($source,PDO::PARAM_STR),':page' => array($numpage,PDO::PARAM_INT),':language' => array($language,PDO::PARAM_STR)));
        $result=$this->con->getResults();
        return $result;
    }

    public function searchPers(string $Carac,int $numpage,string $language):array{

        //recherche par personnage
        $query="SELECT q.id_quote, q.content, c.caracter, c.img_path, s.title, s.date, q.like, q.language FROM Quote q JOIN Caracter c ON c.id_caracter = q.id_caracter JOIN Source s ON s.id_source = q.id_source  WHERE c.caracter LIKE '%:pers%' AND q.isValid = true AND language = :language LIMIT 20 OFFSET :page*20;";
        $this->con->executeQuery($query,array(':pers' => array($Pers,PDO::PARAM_STR),':page' => array($numpage,PDO::PARAM_INT),':language' => array($language,PDO::PARAM_STR)));
        $result=$this->con->getResults();
        return $result;
    }

    public function searchId(string $id):array{

        //recherche par id
        $query="SELECT q.id_quote, q.content, c.caracter, i.imgPath, s.title, s.dates, q.likes, q.langue FROM Quote q JOIN Caracter c ON c.id_caracter = q.id_caracter JOIN Source s ON s.id_source = q.id_source JOIN Image i ON c.id_img = i.id_img WHERE q.id_quote = :id AND q.isvalide = true;";
        $this->con->executeQuery($query,array(':id' => array($id,PDO::PARAM_STR)));
        $result=$this->con->getResults();
        return $result;
    }

    public function search(?string $type,?string $search,array $filtre):array{
        $query="SELECT q.id_quote, q.content, c.caracter, i.imgPath, s.title, s.dates, q.likes, q.langue 
                FROM Quote q 
                JOIN Caracter c ON c.id_caracter = q.id_caracter 
                JOIN Source s ON s.id_source = q.id_source 
                JOIN Image i ON c.id_img = i.id_img 
                WHERE lower(";
        
        if($type=='personnage'){
            $query = $query . "c.caracter) LIKE lower('%" . $search . "%')";
        }
        elseif($type=='titre'){
            $query = $query . "s.title) LIKE lower('%" . $search . "%')";
        }
        else{
            $query = $query . "q.content) LIKE lower('%" . $search . "%')";
        }
        /* Categorie a rajouter
        foreach($filtre as $fil){
            $query = $query . " AND " . $fil
        }*/

        $this->con->executeQuery($query,array());
        
        $result=$this->con->getResults();
        return $result;
    }

    public function getQuoteOfTheDay(string $language): array {
        $query = "SELECT q.id_quote, q.content, c.caracter, i.imgPath, s.title, s.dateS, q.likes, q.langue
                  FROM Quote q
                  JOIN Caracter c ON c.id_caracter = q.id_caracter
                  JOIN Source s ON s.id_source = q.id_source
                  JOIN Image i ON c.id_img = i.id_img
                  WHERE q.isValide = true AND q.isCitationDuJour = true AND q.langue = :language
                  ORDER BY id_quote DESC LIMIT 1;";
        try {
            $this->con->executeQuery($query, [':language' => [$language, PDO::PARAM_STR]]);
            $result = $this->con->getResults();
            return $result[0] ?? [];
        } catch (PDOException $e) {
            echo "Erreur dans getQuoteOfTheDay: " . $e->getMessage();
            return [];
        }
    }    
    

    public function getSuggestions(int $numpage, string $language): array {
        $query = "SELECT q.id_quote, q.content, c.caracter, i.imgPath, s.title, s.dateS, q.likes, q.langue
                  FROM Quote q
                  JOIN Caracter c ON c.id_caracter = q.id_caracter
                  JOIN Source s ON s.id_source = q.id_source
                  JOIN Image i ON c.id_img = i.id_img
                  WHERE q.isValide = true AND q.langue = :language
                  ORDER BY RANDOM()
                  LIMIT 20 OFFSET :offset;";

        $this->con->executeQuery($query, [
            ':language' => [$language, PDO::PARAM_STR],
            ':offset' => [$numpage * 20, PDO::PARAM_INT]
        ]);
        return $this->con->getResults();
    }

    public function getFavorites(string $userId): array {
        $query = "SELECT q.id_quote, q.content, c.caracter, i.imgPath, s.title, s.dateS, q.likes, q.langue
                  FROM Favorite f
                  JOIN Quote q ON f.quote = q.id_quote
                  JOIN Caracter c ON c.id_caracter = q.id_caracter
                  JOIN Source s ON s.id_source = q.id_source
                  JOIN Image i ON c.id_img = i.id_img
                  WHERE f.users = :userId";
    
        try {
            $this->con->executeQuery($query, [
                ':userId' => [$userId, PDO::PARAM_STR]
            ]);
            return $this->con->getResults();
        } catch (PDOException $e) {
            echo "Erreur dans getFavorites: " . $e->getMessage();
            return [];
        }
    }

    //======================== PARTI ADMIN ========================
    //Probablement à déplacer dans un autre fichier

    public function getQuoteIsValide():array{
        //obtenir les quotes en attentes de validation par l'admin
        $query = 'SELECT *  FROM Quote WHERE isValid=:bool';
        $this->con->executeQuery($query,array(':bool' => array(false, PDO::PARAM_BOOL)));
        $result=$this->con->getResults();
        return $result;
    }

    public function validQuote(int $id){
        //Valider la quote par l'admin
        $query ='UPDATE Quote SET isValid=:newValide WHERE id_Quote=:id';
        $this->con->executeQuery($query,array(':id' => array($id,PDO::PARAM_INT)));
    }

    public function invalidQuote(int $id){
        //Invalide la quote par l'admin (suppression)
        $query ='DELETE FROM Quote WHERE id_Quote=:id';
        $this->con->executeQuery($query,array(':id' => array($id,PDO::PARAM_INT)));
    }

    public function updateContent(int $id, string $newContent):array{
        //Update le contexte de quote passé en paramètre
        $queryUpdate = 'UPDATE Quote SET content=:newContent WHERE id_quote=:idQuote';
        $con->executeQuery($queryUpdate, array(':idQuote'=>array($id, PDO::PARAM_STR), ':newContent'=> array($newContent, PDO::PARAM_STR)));

        //Renvoie le nouveau contexte de quote
        $queryReponse = 'SELECT content FROM Quote WHERE id_quote=:idQuote';    
        $con->executeQuery($queryReponse, array(':idQuote'=>array($id, PDO::PARAM_STR)));
        $result = $con->getResults();
        return $result;
    }

    public function updateTimeCode(int $id, string $newTimeCode):array{
        //Update le time code de quote passé en paramètre
        $queryUpdate = 'UPDATE Quote SET timecode=:newTimeCode WHERE id_quote=:idQuote';
        $con->executeQuery($queryUpdate, array(':idQuote'=>array($id, PDO::PARAM_STR), ':newTimeCode'=> array($newTimeCode, PDO::PARAM_STR)));

        //Renvoie le nouveau contexte de quote
        $queryReponse = 'SELECT timecode FROM Quote WHERE id_quote=:idQuote';    
        $con->executeQuery($queryReponse, array(':idQuote'=>array($id, PDO::PARAM_STR)));
        $result = $con->getResults();
        return $result;
    }

    public function updateDate(int $id, int $newDate):array{
        //Update la date de quote passé en paramètre
        $queryUpdate = 'UPDATE Source SET dateSource =:newdate WHERE idSource = (SELECT idSource FROM Quote WHERE idQuote =:idQuote)';
        $con->executeQuery($queryUpdate, array(':idQuote'=>array($id, PDO::PARAM_STR), ':newDate'=> array($newDate, PDO::PARAM_INT)));

        //Renvoie la nouvelle date de quote
        $queryReponse = 'SELECT s.dateSource FROM Source s, Quote q WHERE id_quote=:idQuote AND s.idSource = q.idSource';    
        $con->executeQuery($queryReponse, array(':idQuote'=>array($id, PDO::PARAM_STR)));
        $result = $con->getResults();
        return $result;
    }

    public function updateTitle(int $id, string $newTitle):array{
        //Update le titre de l'oeuvre de quote passé en paramètre
        $queryUpdate = 'UPDATE Source SET title =:newTitle WHERE idSource = (SELECT idSource FROM Quote WHERE idQuote =:idQuote)';
        $con->executeQuery($queryUpdate, array(':idQuote'=>array($id, PDO::PARAM_STR), ':newTitle'=> array($newTitle, PDO::PARAM_STR)));

        //Renvoie le nouveau titre de quote
        $queryReponse = 'SELECT s.title FROM Source s, Quote q WHERE id_quote=:idQuote AND s.idSource = q.idSource';    
        $con->executeQuery($queryReponse, array(':idQuote'=>array($id, PDO::PARAM_STR)));
        $result = $con->getResults();
        return $result;
    }

    public function updateNameCharacter(int $id, string $newNameCharacter):array{
        //Update le personnage de l'oeuvre de quote passé en paramètre
        $queryUpdate = 'UPDATE Character SET name =:newNameCharacter WHERE idCharacter = (SELECT idCharacter FROM Quote WHERE idQuote =:idQuote)';
        $con->executeQuery($queryUpdate, array(':idQuote'=>array($id, PDO::PARAM_STR), ':newNameCharacter'=> array($newNameCharacter, PDO::PARAM_STR)));

        //Renvoie le nouveau personnage de quote
        $queryReponse = 'SELECT c.title FROM Character c, Quote q WHERE id_quote=:idQuote AND c.idCharacter = q.idCharacter';    
        $con->executeQuery($queryReponse, array(':idQuote'=>array($id, PDO::PARAM_STR)));
        $result = $con->getResults();
        return $result;
    }
}

?>