<?php
namespace Gateway;

use PDO;

class AccueilGateway {

    private Connection $con;

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

    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();
    }
}
