<?php
class Group {
    private $id;
    private $name;
    private $description;

    public function loadFromId($id) {
        global $wpdb;

        $sql = "
            SELECT IDGroup, name, description FROM cass_group WHERE IDGroup = %d;
        ";
        $result = $wpdb->get_row($wpdb->prepare($sql, $id));

        if ($result) {
            $this->setId($result->IDGroup);
            $this->setName($result->name);
            $this->setDescription($result->description);
            return true;
        } else {
            return false;
        }
    }

    public static function selectAll() {
        global $wpdb;

        $sql = "
            SELECT IDGroup, name, description 
            FROM cass_group;
             ";
     
        return $wpdb->get_results($sql);
    }

    // Méthode pour mettre à jour les données du groupe
    public function update() {
        global $wpdb;

        $sql = "
            UPDATE cass_group
            SET name = %s, description = %s
            WHERE IDGroup = %d;
         ";
         $wpdb->query($wpdb->prepare($sql, $this->getName(), $this->getDescription(), $this->getId()));
    }

    // Méthode pour supprimer le groupe
    public function delete() {
        global $wpdb;

        $sql = "
            DELETE FROM cass_group
            WHERE IDGroup = %d;
         ";
         $wpdb->query($wpdb->prepare($sql, $this->getId()));
    }

    public function create() {
        global $wpdb;

        $sql = "
        INSERT INTO cass_group(name, description)
        VALUES(%s,%s);
         ";
         $wpdb->query($wpdb->prepare($sql, $this->getName(), $this->getDescription()));
    }

    public function sqlWPTable() {
        global $wpdb;

        $sql = "
            SELECT IDGroup, name, description from cass_group;
             ";
     
        return $wpdb->get_results(
            $sql,
            ARRAY_A
        );
    }

    // Getter and setters
    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    public function getDescription() {
        return $this->description;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function setDescription($description) {
        $this->description = $description;
    }
}
