<?php 
class Member
{
    private $ID;
    private $role;
    private $lastname;
    private $firstname;
    private $email;
    private $pseudonyme;
    private $cassMemberNo;
    private $cassLevel;
    private $cassLevelTechSki;
    private $cassLevelClimbing;
    private $cassLevelMountaineering;
    private $cassGroup;
    private $cassTelephone;
    private $cassAddress;
    private $cassNPA;
    private $cassLocality;

    public function __construct($user_id)
    {
        // Récupérer les informations de l'utilisateur
        $user = get_userdata($user_id);
        
        $this->ID = $user->ID;
        $roles = implode(', ', $user->roles); // Récupérer les rôles
 
        if (in_array('CASS_RL', $user->roles)) {
            $this->role = 'Chef de course';
        } else {
            $this->role = 'Membre';
        }

        $this->lastname = $user->last_name;
        $this->firstname = $user->first_name;
        $this->email = $user->user_email;
        $this->pseudonyme = $user->user_nicename;

        // Récupérer les métadonnées
        $this->cassMemberNo = sanitize_text_field(get_user_meta($user_id, 'cassMemberNo', true));
        $cassLevelId = sanitize_text_field(get_user_meta($user_id, 'cassLevel', true));
        $level = new Level();
        $level->loadFromId($cassLevelId);
        $this->cassLevel = $level->getName();

        $cassLevelTechSkiId = sanitize_text_field(get_user_meta($user_id, 'cassLevelTechSki', true));
        $levelTechSki = new Level();
        $levelTechSki->loadFromId($cassLevelTechSkiId);
        $this->cassLevelTechSki = $levelTechSki->getName();

        $cassLevelClimbingId = sanitize_text_field(get_user_meta($user_id, 'cassLevelClimbing', true));
        $levelClimbing = new Level();
        $levelClimbing->loadFromId($cassLevelClimbingId);
        $this->cassLevelClimbing = $levelClimbing->getName();

        $cassLevelMountaineeringId = sanitize_text_field(get_user_meta($user_id, 'cassLevelMountaineering', true));
        $levelMountaineering = new Level();
        $levelMountaineering->loadFromId($cassLevelMountaineeringId);
        $this->cassLevelMountaineering = $levelMountaineering->getName();
        
        $GroupID = sanitize_text_field(get_user_meta($user_id, 'cassGroup', true));

        // Récupérer le nom du groupe
        $group = new Group();
        $group->loadFromId($GroupID);
        $this->cassGroup = $group->getName();

        $this->cassTelephone = sanitize_text_field(get_user_meta($user_id, 'cassTelephone', true));
        $this->cassAddress = sanitize_text_field(get_user_meta($user_id, 'cassAddress', true));
        $this->cassNPA = sanitize_text_field(get_user_meta($user_id, 'cassNPA', true));
        $this->cassLocality = sanitize_text_field(get_user_meta($user_id, 'cassLocality', true));
    }

    // Méthode qui récupère la liste des utilisateurs avec leurs métadonnées
    public static function sqlWPTable()
    {
        global $wpdb;

        // Argument pour récupérer les utilisateurs avec le rôle 'CASS_MEMBER'
        $args = array(
            'role' => 'CASS_MEMBER',
            'orderby' => 'user_nicename',
            'order' => 'ASC'
        );

        // Récupérer tous les utilisateurs
        $users = get_users($args);
        $members = [];

        // Pour chaque utilisateur, récupérer ses métadonnées et les ajouter à la liste
        foreach ($users as $user) {
            $members[] = new Member($user->ID);
        }

        // Retourner la liste des membres
        return $members;
    }

    // Méthodes getter pour chaque propriété
    public function getID() {
        return $this->ID;
    }

    public function getRole() {
        return $this->role;
    }

    public function getLastname() {
        return $this->lastname;
    }

    public function getFirstname() {
        return $this->firstname;
    }

    public function getEmail() {
        return $this->email;
    }

    public function getPseudonyme() {
        return $this->pseudonyme;
    }

    public function getCassMemberNo() {
        return $this->cassMemberNo;
    }

    public function getCassLevel() {
        return $this->cassLevel;
    }

    public function getCassLevelTechSki() {
        return $this->cassLevelTechSki;
    }

    public function getCassLevelClimbing() {
        return $this->cassLevelClimbing;
    }

    public function getCassLevelMountaineering() {
        return $this->cassLevelMountaineering;
    }

    public function getCassGroup() {
        return $this->cassGroup;
    }

    public function getCassTelephone() {
        return $this->cassTelephone;
    }

    public function getCassAddress() {
        return $this->cassAddress;
    }

    public function getCassNPA() {
        return $this->cassNPA;
    }

    public function getCassLocality() {
        return $this->cassLocality;
    }
}
