<?php

namespace Database;
use \PDO;
use Shared\Log;

class AthleteGateway {
    private Connexion $connection;

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

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

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

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

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

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

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

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

    public function getAthleteByWeight(int $weight): array
    {
        $query = "SELECT * FROM Athlete WHERE poids = :weight AND isCoach=FALSE";
        $params = [':weight' => [$weight, PDO::PARAM_INT]];
        $res = $this->connection->executeWithErrorHandling($query, $params);
        return $res;
    }

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

    public function addAthlete(AthleteEntity $athlete): array
    {
        $query = "INSERT INTO Athlete (nom, prenom, email, sexe, taille, poids, motDePasse, dateNaissance, isCoach) 
                  VALUES (:nom, :prenom, :email, :sexe, :taille, :poids, :motDePasse, :dateNaissance, :isCoach)";

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

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

    public function updateAthlete(AthleteEntity $oldAthlete, AthleteEntity $newAthlete): array
    {
        $query = "UPDATE Athlete 
                  SET nom = :nom, prenom = :prenom, email = :email, sexe = :sexe,
                  taille = :taille, poids = :poids, motDePasse = :motDePasse, dateNaissance = :dateNaissance, isCoach = :isCoach
                  WHERE idAthlete = :idAthlete";

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

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

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

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

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



}


// Exemple d'utilisation
//$dsn = "pgsql:host=localhost;port=5432;dbname=mydatabase;user=myuser;password=mypassword";
//$connection = new Connection($dsn);
//$gateway = new AthleteGateway($connection);

//$allAth = $gateway->getAthlete();
//print_r($allAth);

//$singleAth = $gateway->getAthleteById(1);
//print_r($singleAth);

?>
