<?php

class Reponse {

    private $Id_reponse;
    private $libelle;
    private $Id_sondage;

    public function __construct($Id_reponse = null, $libelle = null, $Id_sondage = null) {
        $this->Id_reponse = $Id_reponse;
        $this->libelle = $libelle;
        $this->Id_sondage = $Id_sondage;
    }

    public function getId_reponse() {
        return $this->Id_reponse;
    }

    public function setId_reponse($Id_reponse) {
        $this->Id_reponse = $Id_reponse;
    }

    public function getLibelle() {
        return $this->libelle;
    }

    public function setLibelle($libelle) {
        $this->libelle = $libelle;
    }

    public function getId_sondage() {
        return $this->Id_sondage;
    }

    public function setId_sondage($Id_sondage) {
        $this->Id_sondage = $Id_sondage;
    }

    public static function getByIdSondage($Id_sondage) {
        $db = Bdd::getInstance();
        $pdo = $db->getConnection();  
        try {
            $stmt = $pdo->prepare('SELECT * FROM reponse WHERE Id_sondage = :Id_sondage');
            $stmt->execute(['Id_sondage' => $Id_sondage]);
            $response_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
            $responses = [];
            foreach ($response_data as $data) {
                $responses[] = new Reponse($data['Id_reponse'], $data['libelle'], $data['Id_sondage']);
            }
            return $responses;
        } catch (PDOException $e) {
            echo 'Error: ' . $e->getMessage();
            return [];
        }
    }
    public static function getByIdSondagedysplaiable($Id_sondage) {
        $db = Bdd::getInstance();
        $pdo = $db->getConnection();  
        try {
            $stmt = $pdo->prepare('SELECT count(*) as nb , Id_sondage, libelle FROM reponse WHERE Id_sondage = :Id_sondage group by libelle,Id_sondage');
            $stmt->execute(['Id_sondage' => $Id_sondage]);
            $response_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
            $responses = [];
            foreach ($response_data as $data) {
                $responses[] = array($data['nb'], $data['Id_sondage'],$data['libelle']);
            }
            return $responses;
        } catch (PDOException $e) {
            echo 'Error: ' . $e->getMessage();
            return [];
        }
    }
    public static function getnbresponsuser($Id_sondage, $u) {
        $db = Bdd::getInstance();
        $pdo = $db->getConnection();
        try {
            $stmt = $pdo->prepare('SELECT count(*) as nb , Id_sondage, libelle FROM reponse WHERE Id_sondage = :Id_sondage AND Id_utilisateur = :id_user group by libelle, Id_sondage');
            $stmt->execute(['Id_sondage' => $Id_sondage, 'id_user' => $u]);
            $response_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
            $responses = [];
            foreach ($response_data as $data) {
                $responses[] = array($data['nb'], $data['Id_sondage'], $data['libelle']);
            }
            return $responses;
        } catch (PDOException $e) {
            echo 'Error: ' . $e->getMessage();
            return [];
        }
    }
    public function save() {
        // Obtain the database connection
        $db = Bdd::getInstance();
        $pdo = $db->getConnection();  

        try {
            $stmt = $pdo->prepare('INSERT INTO reponse (libelle, Id_utilisateur , Id_sondage) VALUES (:libelle, :id_utilisateur, :Id_sondage)');
            $stmt->execute([
                'libelle' => $this->libelle,
                'Id_sondage' => $this->Id_sondage,
                'id_utilisateur' => $_SESSION['user']->getId_utilisateur()
            ]);
            // Set the ID of the created response
            $this->Id_reponse = $pdo->lastInsertId();

            return true;

        } catch (PDOException $e) {
            echo 'Error: ' . $e->getMessage();
            return false;
        }
    }
}
