<?php
    class UserModel {
		public static function createUser($post) {
			if(isset($post['name']) && isset($post['email'])) {
				$request = DatabaseConnector::current()->prepare("INSERT INTO `users` (`id`, `name`, `email`) VALUES (NULL, '" . $post['name'] . "', '" . $post['email'] . "')");
				$request->execute();
				return [http_response_code(200), "Utilisateur ajouté"];
			}
			else {
				return [http_response_code(400), "Les paramètres fournis sont incorrects"];
			}
		}

        public static function getUser($id) {
			$request = DatabaseConnector::current()->prepare("SELECT * FROM `users` WHERE `id` = " . $id);
			$request->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, get_called_class());
			$request->execute();
			$user = $request->fetchAll();

			if(!empty($user)) {
				return [http_response_code(200), $user];
			}
			else {
				return [http_response_code(400), "L'utilisateur n'existe pas'"];
			}
		}

        public static function getAllUsers() {
			$request = DatabaseConnector::current()->prepare("select * from users");
			$request->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, get_called_class());
			$request->execute();
			$allUsers = $request->fetchAll();
			return [http_response_code(200), $allUsers];
		}
		
        public static function updateUser($post, $id) {
			$request = DatabaseConnector::current()->prepare("SELECT * FROM `users` WHERE `id` = " . $id);
			$request->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, get_called_class());
			$request->execute();
			$user = $request->fetchAll();

			if(!empty($user)) {
				$quest = "";

				if (isset($post->name) && isset($post->email))
					$quest = "UPDATE `users` SET `name` = '" . $post->name . "', `email` = '" . $post->email . "' WHERE `users`.`id` = " . $id;
				else if (isset($post->name) && !isset($post->email))
					$quest = "UPDATE `users` SET `name` = '" . $post->name . "' WHERE `users`.`id` = " . $id;
				else if (!isset($post->name) && isset($post->email))
					$quest = "UPDATE `users` SET `email` = '" . $post->email . "' WHERE `users`.`id` = " . $id;

				if ($quest != "") {
					$request = DatabaseConnector::current()->prepare($quest);
					$request->execute();
					return [http_response_code(200), "Utilisateur modifié"];
				}
				else
					return [http_response_code(400), "Les paramètres fournis sont incorrects"];
			}
			else {
				return [http_response_code(400), "L'utilisateur n'existe pas"];
			}

		}
		
        public static function deleteUser($id) {
			$request = DatabaseConnector::current()->prepare("SELECT * FROM `users` WHERE `id`=" . $id);
			$request->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, get_called_class());
			$request->execute();
			$user = $request->fetchAll();

			if(!empty($user)) {
				$request = DatabaseConnector::current()->prepare("DELETE FROM `users` WHERE `id` =" . $id);
				$request->execute();
				return [http_response_code(200), "Utilisateur supprimé"];
			}
			else {
				return [http_response_code(400), "L'utilisateur n'existe pas"];
			}
		}
    }
?>