<?php
namespace Gateway;
use Connection;

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 getComment(int $id):array{
        
        //obtention des commentaire d'une citation
        $query="SELECT c.id_comment u.username, u.imgPath, c.comment, c.date FROM Commentary c JOIN Quote q ON c.quote = q.id_quote JOIN User u ON u.id_user = c.user JOIN Image i ON i.id_img = u.img WHERE id_quote = :id;";
        $this->con->executeQuery($query,array(':id' => array($id,PDO::PARAM_INT)));
        $result=$this->con->getResults();
        return $result;
    }

}

?>
