DROP PROCEDURE IF EXISTS PresentProject;
DELIMITER //
CREATE PROCEDURE PresentProject
(
  IN p_sid INT,
  IN p_password TEXT,
  IN p_pid INT,
  OUT outputCode INT(11),
  OUT o_public INT(1),
  OUT o_name VARCHAR(255),
  OUT o_ownerID INT,
  OUT o_ownerPseudo VARCHAR(255)
  
)
MODIFIES SQL DATA
BEGIN

CALL `CheckIdLogin`(p_sid,p_password, outputCode);
IF outputCode = 42 OR outputCode = 2005 THEN 
	SET outputCode := (SELECT (CASE WHEN (SELECT CheckInGroup(p_sid,authorizedGroupId) = 42 OR CheckInGroup(p_sid,secondaryOwnersGroupId) = 42 OR p_sid = ownerID FROM projets WHERE ID = p_pid) THEN 42 ELSE 2017 END));
	IF outputCode = 42 THEN
		SELECT authorizedGroupId = 1 AS public, name INTO o_public, o_name FROM projets WHERE projets.ID = p_pid;
		SELECT membres.pseudo, membres.ID INTO o_ownerPseudo, o_ownerID FROM membres INNER JOIN projets ON projets.ownerID = membres.ID WHERE projets.ID = p_pid;
		INSERT INTO verzions SELECT languageID AS language, ID, name FROM versions WHERE projectID = p_pid;
		INSERT INTO sowners SELECT pseudo FROM membres INNER JOIN projets ON projets.ownerID != membres.ID AND CheckInGroup(membres.ID,projets.secondaryOwnersGroupID) = 42 WHERE projets.ID = p_pid;
	END IF;
END IF;

END//
DELIMITER ;
