<?php

namespace Database;
use \PDO;

class ActivityGateway {
    private Connexion $connection;

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

    public function getActivity() {
        $query = "SELECT * FROM Activite";
        $res = $this->connection->executeWithErrorHandling($query);
        return $res;
    }

    public function getActivityById(int $activityId) {
        $query = "SELECT * FROM Activite WHERE idActivite = :id";
        $params = [':id' => [$activityId, PDO::PARAM_INT]];
        $res = $this->connection->executeWithErrorHandling($query, $params);
        return $res;
    }

    public function getActivityByType(string $type) {
        $query = "SELECT * FROM Activite WHERE type = :type";
        $params = [':type' => [$type, PDO::PARAM_STR]];
        $res = $this->connection->executeWithErrorHandling($query, $params);
        return $res;
    }

    public function getActivityByDate(string $date) {
        $query = "SELECT * FROM Activite WHERE date = :date";
        $params = [':date' => [$date, PDO::PARAM_STR]];
        $res = $this->connection->executeWithErrorHandling($query, $params);
        return $res;
    }

    public function getActivityByTimeRange(string $startTime, string $endTime) {
        $query = "SELECT * FROM Activite WHERE heureDebut >= :startTime AND heureFin <= :endTime";
        $params = [
            ':startTime' => [$startTime, PDO::PARAM_STR],
            ':endTime' => [$endTime, PDO::PARAM_STR]
        ];
        $res = $this->connection->executeWithErrorHandling($query, $params);
        return $res;
    }

    public function getActivityByEffort(int $effortRessenti) {
        $query = "SELECT * FROM Activite WHERE effortRessenti = :effort";
        $params = [':effort' => [$effortRessenti, PDO::PARAM_INT]];
        $res = $this->connection->executeWithErrorHandling($query, $params);
        return $res;
    }

    public function getActivityByVariability(int $variabilite) {
        $query = "SELECT * FROM Activite WHERE variabilite = :variability";
        $params = [':variability' => [$variabilite, PDO::PARAM_INT]];
        $res = $this->connection->executeWithErrorHandling($query, $params);
        return $res;
    }

    public function getActivityByTemperature(int $temperatureMoyenne) {
        $query = "SELECT * FROM Activite WHERE temperatureMoyenne = :temperature";
        $params = [':temperature' => [$temperatureMoyenne, PDO::PARAM_INT]];
        $res = $this->connection->executeWithErrorHandling($query, $params);
        return $res;
    }

    public function addActivity(ActivityEntity $activity) {
        $query = "INSERT INTO Activite (type, date, heureDebut, heureDeFin, effortRessenti, variabilite, variance, ecartType, moyenne, maximum, minimum, temperatureMoyenne) 
                  VALUES (:type, :date, :heureDebut, :heureDeFin, :effortRessenti, :variabilite, :variance, :ecartType, :moyenne, :maximum, :minimum, :temperatureMoyenne)";

        $params = [
            ':type' => $activity->getType(),
            ':date' => $activity->getDate()->format('Y-m-d'), // Format date pour SQL
            ':heureDebut' => $activity->getHeureDebut()->format('H:i:s'), // Format heure pour SQL
            ':heureDeFin' => $activity->getHeureFin()->format('H:i:s'), // Format heure pour SQL
            ':effortRessenti' => $activity->getEffortRessenti(),
            ':variabilite' => $activity->getVariabilite(),
            ':variance' => $activity->getVariance(),
            ':ecartType' => $activity->getEcartType(),
            ':moyenne' => $activity->getMoyenne(),
            ':maximum' => $activity->getMaximum(),
            ':minimum' => $activity->getMinimum(),
            ':temperatureMoyenne' => $activity->getTemperatureMoyenne(),
        ];

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

    public function updateActivity(ActivityEntity $oldActivity, ActivityEntity $newActivity) {
        $query = "UPDATE Activite
                  SET type = :type, date = :date, heureDebut = :heureDebut, heureDeFin = :heureDeFin,
                  effortRessenti = :effortRessenti, variabilite = :variabilite, variance = :variance, ecartType = :ecartType, moyenne = :moyenne, maximum = :maximum, minimum = :minimum, temperatureMoyenne = :temperatureMoyenne
                  WHERE idActivite = :idActivite";

        $params = [
            ':idActivite' => $oldActivity->getIdActivity(),
            ':type' => $newActivity->getType(),
            ':date' => $newActivity->getDate()->format('Y-m-d'), // Format date pour SQL
            ':heureDebut' => $newActivity->getHeureDebut()->format('H:i:s'), // Format heure pour SQL
            ':heureDeFin' => $newActivity->getHeureFin()->format('H:i:s'), // Format heure pour SQL
            ':effortRessenti' => $newActivity->getEffortRessenti(),
            ':variabilite' => $newActivity->getVariabilite(),
            ':variance' => $newActivity->getVariance(),
            ':ecartType' => $newActivity->getEcartType(),
            ':moyenne' => $newActivity->getMoyenne(),
            ':maximum' => $newActivity->getMaximum(),
            ':minimum' => $newActivity->getMinimum(),
            ':temperatureMoyenne' => $newActivity->getTemperatureMoyenne(),
        ];

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

    public function deleteActivity(int $idActivity) {
        $query = "DELETE FROM Activite WHERE idActivite = :idActivity";

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

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

?>
