<?php

namespace Database;
use \PDO;

class CoachGateway {
    private $connection;

    public function __construct(Connexion $connection) {
        $this->connection = $connection;
    }

    public function getCoach() {
        $query = "SELECT * FROM Athlete WHERE isCoach = TRUE;";
        $res = $this->connection->executeWithErrorHandling($query);
        return $res;
    }

    public function getCoachById(int $userId) {
        $query = "SELECT * FROM Athlete WHERE idAthlete = :id AND isCoach = TRUE";
        $params = [':id' => [$userId, PDO::PARAM_INT]];
        $res = $this->connection->executeWithErrorHandling($query, $params);
        return $res;
    }

    public function getCoachByName(string $name) {
        $query = "SELECT * FROM Athlete WHERE nom = :name AND isCoach = TRUE";
        $params = [':name' => [$name, PDO::PARAM_STR]];
        $res = $this->connection->executeWithErrorHandling($query, $params);
        return $res;
    }

    public function getCoachByFirstName(string $firstName) {
        $query = "SELECT * FROM Athlete WHERE prenom = :firstName AND isCoach = TRUE";
        $params = [':firstName' => [$firstName, PDO::PARAM_STR]];
        $res = $this->connection->executeWithErrorHandling($query, $params);
        return $res;
    }

    public function getCoachByEmail(string $email) {
        $query = "SELECT * FROM Athlete WHERE email = :email AND isCoach = TRUE";
        $params = [':email' => [$email, PDO::PARAM_STR]];
        $res = $this->connection->executeWithErrorHandling($query, $params);
        return $res;
    }

    public function getCoachByGender(string $gender) {
        $query = "SELECT * FROM Athlete WHERE sexe = :gender AND isCoach = TRUE";
        $params = [':gender' => [$gender, PDO::PARAM_STR]];
        $res = $this->connection->executeWithErrorHandling($query, $params);
        return $res;
    }

    public function getCoachByHeight(int $height) {
        $query = "SELECT * FROM Athlete WHERE taille = :height AND isCoach = TRUE";
        $params = [':height' => [$height, PDO::PARAM_INT]];
        $res = $this->connection->executeWithErrorHandling($query, $params);
        return $res;
    }

    public function getCoachByBirthDate(string $birthdate) {
        $query = "SELECT * FROM Athlete WHERE dateNaissance = :birthdate AND isCoach = TRUE";
        $params = [':birthdate' => [$birthdate, PDO::PARAM_STR]];
        $res = $this->connection->executeWithErrorHandling($query, $params);
        return $res;
    }

    public function addCoach(CoachEntity $coach) {
        $query = "INSERT INTO Athlete (nom, prenom, email, sexe, taille, poids, motDePasse, dateNaissance, isCoach) 
                  VALUES (:nom, :prenom, :email, :sexe, :taille, :poids, :motDePasse, :dateNaissance, TRUE)";

        $params = [
            ':nom' => $coach->getNom(),
            ':prenom' => $coach->getPrenom(),
            ':email' => $coach->getEmail(),
            ':sexe' => $coach->getSexe(),
            ':taille' => $coach->getTaille(),
            ':poids' => $coach->getPoids(),
            ':motDePasse' => $coach->getMotDePasse(),
            ':dateNaissance' => $coach->getDateNaissance(), 
        ];

        $res = $this->connection->executeWithErrorHandling($query, $params);
        return $res;
    }

    public function updateCoach(CoachEntity $oldCoach, CoachEntity $newCoach) {
        $query = "UPDATE Athlete 
                  SET nom = :nom, prenom = :prenom, email = :email, sexe = :sexe,
                  taille = :taille, poids = :poids, motDePasse = :motDePasse, dateNaissance = :dateNaissance, isCoach = TRUE
                  WHERE idCoach = :idCoach";

        $params = [
            ':idAthlete' => $oldCoach->getIdCoach(),
            ':nom' => $newCoach->getNom(),
            ':prenom' => $newCoach->getPrenom(),
            ':email' => $newCoach->getEmail(),
            ':sexe' => $newCoach->getSexe(),
            ':taille' => $newCoach->getTaille(),
            ':poids' => $newCoach->getPoids(),
            ':motDePasse' => $newCoach->getMotDePasse(),
            ':dateNaissance' => $newCoach->getDateNaissance(), 
        ];

        $res = $this->connection->executeWithErrorHandling($query, $params);
        return $res;
    }

    public function deleteCoach(int $idCoach) {
        $query = "DELETE FROM Athlete WHERE idAthlete = :idCoach";

        $params = [
            ':idCoach' => $idCoach,
        ];

        $res = $this->connection->executeWithErrorHandling($query, $params);
        return $res;
    }

}