--------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------- DECLARATION DES VARIABLES --------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION GET_VAR(variable varchar, addedValue int) RETURNS int LANGUAGE plpgsql AS $$
DECLARE
	customer int;
	pressTitle int;
	cahier int;
	edition int;
	sys_role int;
	sys_group int;
	sys_user int;
	sys_thesaurus int;
	sys_color int;
BEGIN
	---- change ID here ----
	customer    	:= 2000 + addedValue;
	pressTitle 		:= 2000 + addedValue;
	cahier 			:= 2000 + addedValue;
	edition 		:= 2000 + addedValue;
	sys_role 		:= 2000 + addedValue;
	sys_group 		:= 2000 + addedValue;
	sys_user 		:= 10000 + addedValue;
	sys_thesaurus	:= 2000 + addedValue;
	sys_color		:= 2000 + addedValue;
	------------------------
	CASE
	  WHEN variable = 'customer' 		THEN RETURN customer;
	  WHEN variable = 'pressTitle' 		THEN RETURN pressTitle;
	  WHEN variable = 'cahier' 			THEN RETURN cahier;
	  WHEN variable = 'edition' 		THEN RETURN edition;
	  WHEN variable = 'sys_role' 		THEN RETURN sys_role;
	  WHEN variable = 'sys_group' 		THEN RETURN sys_role;
	  WHEN variable = 'sys_user' 		THEN RETURN sys_role;
	  WHEN variable = 'sys_thesaurus' 	THEN RETURN sys_thesaurus;
	  WHEN variable = 'sys_color' 		THEN RETURN sys_color;
	  ELSE RETURN 0;
	END CASE;
END
$$;
--------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------


-- AJOUT sys_customer
INSERT INTO sys_customer(id, name, code)
    VALUES (GET_VAR('customer', 0), 'Test Nouveau Customer', 'TEST');
SELECT pg_catalog.setval('sys_customer_id_seq', GET_VAR('customer', 50), true);

-- AJOUT sys_customer_has_items (Ici: ajoute tous les états, action et transitions au nouveau customer)
INSERT INTO sys_customer_has_items("customerId", "className", "objectId", "order")
	SELECT GET_VAR('customer', 0), 'sys_transition', id, 0 FROM sys_transition;
INSERT INTO sys_customer_has_items("customerId", "className", "objectId", "order")
	SELECT GET_VAR('customer', 0), 'sys_state', id, 0 FROM sys_state;
INSERT INTO sys_customer_has_items("customerId", "className", "objectId", "order")
	SELECT GET_VAR('customer', 0), 'sys_action', id, 0 FROM sys_action;
 
-- AJOUT pressTitle
INSERT INTO "pressTitle"(
            id, "createdAt", "createdBy", "modifiedAt", "modifiedBy", "notepadAlert", 
            "notepadEdito", "dateAlert", "stateId", name, description, fulltext, 
            autocomplete, "referentFileName", "customerId", code, "multiPageArticle", 
            parameters)
    VALUES (GET_VAR('pressTitle', 0), localtimestamp, -712, localtimestamp, -712, null, 
            null, null, 200, 'Test pressTitle', null, null, 
            null, 'Referent_TEST', GET_VAR('customer', 0), 'TestPT', false, 
            '{ "defaultAnnonceCartonId": "0"}');
SELECT pg_catalog.setval('"editorialGroup_id_seq"', GET_VAR('pressTitle', 50), true);

-- AJOUT cahier
INSERT INTO cahier(
            id, "createdAt", "createdBy", "modifiedAt", "modifiedBy", "notepadAlert", 
            "notepadEdito", "dateAlert", "stateId", name, description, fulltext, 
            autocomplete, "parutionLogicNamePattern", code, "parutionLogicDateBouclageRange", 
            "globalOrder", "pressTitleId", "pressTitleRank", "nbCol", 
            grid,
			guides,
			"docWidth_precision", "docHeight_precision", "marginTop_precision",
			"marginBottom_precision", "marginHorizontal_precision", "paddingTop_precision",
			"paddingBottom_precision", "paddingHorizontal_precision", gutter_precision)
    VALUES (GET_VAR('cahier', 0), localtimestamp, -712, localtimestamp, -712, null, 
            null, null, 29, 'Test cahier', null, null, 
            null, null, 'TestCa', '- 12 hours', 
            10, GET_VAR('pressTitle', 0), 1, 6, 
            '<grid baselineDivision="3.42194444444444" baselineGridRelativeOption="idTopOfPageOfBaselineGridRelativeOption" baselineStart="12.7" baselineViewThreshold="75" documentGridSnapto="True" horizontalGridlineDivision="9.7" horizontalGridSubdivision="8" verticalGridlineDivision="25.4" verticalGridSubdivision="8" />',
			'<guides verticalAlignment="bottom">
				<horizontalGuides>
					<guide position="204 mm"/>
					<guide position="246 mm"/>
					<guide position="289 mm"/>
					<guide position="331 mm"/>
					<guide position="374 mm"/>
				</horizontalGuides>
				<verticalGuides>
					<guide position="10 mm"/>
					<guide position="53 mm"/>
					<guide position="96 mm"/>
					<guide position="139 mm"/>
					<guide position="182 mm"/>
					<guide position="225 mm"/>
				</verticalGuides>
			</guides>',
			274, 388, 26,
			14, 10, 29,
			26, 14, 6);
SELECT pg_catalog.setval('publication_id_seq1', GET_VAR('cahier', 50), true);


-- AJOUT edition
INSERT INTO edition(
            id, "createdAt", "createdBy", "modifiedAt", "modifiedBy", "notepadAlert", 
            "notepadEdito", "dateAlert", "stateId", "cahierId", name, description, 
            fulltext, autocomplete, rank, code)
    VALUES (GET_VAR('edition', 0), localtimestamp, -712, localtimestamp, -712, null, 
            null, null, 32, GET_VAR('cahier', 0), 'Test edition 1', null, 
            null, null, 10, 'TE1'),
			(GET_VAR('edition', 10), localtimestamp, -712, localtimestamp, -712, null, 
            null, null, 32, GET_VAR('cahier', 0), 'Test edition 2', null, 
            null, null, 20, 'TE2');
SELECT pg_catalog.setval('edition_id_seq', GET_VAR('edition', 50), true);


-- AJOUT sys_role
INSERT INTO sys_role(id, name, description, "customerId")
    VALUES (GET_VAR('sys_role', 0), 'Admin test', null, GET_VAR('customer', 0)),
			(GET_VAR('sys_role', 1), 'SGR test', null, GET_VAR('customer', 0)),
			(GET_VAR('sys_role', 2), 'SR test', null, GET_VAR('customer', 0)),
			(GET_VAR('sys_role', 3), 'Monteur test', null, GET_VAR('customer', 0)),
			(GET_VAR('sys_role', 4), 'Journaliste test', null, GET_VAR('customer', 0)),
			(GET_VAR('sys_role', 5), 'Correspondant test', null, GET_VAR('customer', 0));
SELECT pg_catalog.setval('sys_group_id_seq', GET_VAR('sys_role', 50), true);

-- AJOUT sys_group
INSERT INTO sys_group (id, name,description,"customerId", "roleId", "defaultPressTitleId", "bouclageDeltaHours", "groupSystem")
	VALUES (GET_VAR('sys_group', 0), 'Groupe admin test', null, GET_VAR('customer', 0), GET_VAR('sys_role', 0), GET_VAR('pressTitle', 0), 36, false),
			(GET_VAR('sys_group', 1), 'Groupe SRG test', null, GET_VAR('customer', 0), GET_VAR('sys_role', 1), GET_VAR('pressTitle', 0), 36, false),
			(GET_VAR('sys_group', 2), 'Groupe SR test', null, GET_VAR('customer', 0), GET_VAR('sys_role', 2), GET_VAR('pressTitle', 0), 36, false),
			(GET_VAR('sys_group', 3), 'Groupe Monteur test', null, GET_VAR('customer', 0), GET_VAR('sys_role', 3), GET_VAR('pressTitle', 0), 36, false),
			(GET_VAR('sys_group', 4), 'Groupe Journaliste test', null, GET_VAR('customer', 0), GET_VAR('sys_role', 4), GET_VAR('pressTitle', 0), 36, false),
			(GET_VAR('sys_group', 5), 'Groupe Correspondant test', null, GET_VAR('customer', 0), GET_VAR('sys_role', 5), GET_VAR('pressTitle', 0), 36, false);
SELECT pg_catalog.setval('sys_group_id_seq', GET_VAR('sys_group', 50), true);


-- AJOUT sys_user
INSERT INTO sys_user (id, name, mail, enable, password, autosave, "customerId", "groupId")
	VALUES (GET_VAR('sys_user', 0), 'Admin Test', 'TEST@CUSTOM.fr', true, 'melodyadmin', false, GET_VAR('customer', 0), GET_VAR('sys_group', 0));
SELECT pg_catalog.setval('user_id_seq', GET_VAR('sys_user', 50), true);


-- AJOUT sys_role_action
------Admin
INSERT INTO sys_role_action ("actionId", "roleId")
	SELECT id, GET_VAR('sys_role', 0) FROM sys_action WHERE id != 34 AND id != 40 AND id != 25 AND id != 27 AND id != 28;
------SRG
INSERT INTO sys_role_action ("actionId", "roleId")
	VALUES (201, GET_VAR('sys_role', 1)), (29, GET_VAR('sys_role', 1)), (510, GET_VAR('sys_role', 1)), (22, GET_VAR('sys_role', 1)), (810, GET_VAR('sys_role', 1)), (790, GET_VAR('sys_role', 1)), (890, GET_VAR('sys_role', 1)), (955, GET_VAR('sys_role', 1)), (1999, GET_VAR('sys_role', 1)), (120, GET_VAR('sys_role', 1)), (32, GET_VAR('sys_role', 1)), (1000, GET_VAR('sys_role', 1)), (26, GET_VAR('sys_role', 1)), (3000, GET_VAR('sys_role', 1)), (961, GET_VAR('sys_role', 1)), (15, GET_VAR('sys_role', 1)), (90, GET_VAR('sys_role', 1)), (180, GET_VAR('sys_role', 1)), (35, GET_VAR('sys_role', 1)), (300, GET_VAR('sys_role', 1)), (203, GET_VAR('sys_role', 1)), (801, GET_VAR('sys_role', 1)); 
------SR
INSERT INTO sys_role_action ("actionId", "roleId")
	VALUES (510, GET_VAR('sys_role', 2)), (22, GET_VAR('sys_role', 2)), (955, GET_VAR('sys_role', 2)), (1999, GET_VAR('sys_role', 2)), (32, GET_VAR('sys_role', 2)), (26, GET_VAR('sys_role', 2)), (3000, GET_VAR('sys_role', 2)), (15, GET_VAR('sys_role', 2)), (180, GET_VAR('sys_role', 2)), (300, GET_VAR('sys_role', 2)), (203, GET_VAR('sys_role', 2)); 
------Monteur
INSERT INTO sys_role_action ("actionId", "roleId")
	VALUES (29, GET_VAR('sys_role', 3)), (510, GET_VAR('sys_role', 3)), (22, GET_VAR('sys_role', 3)), (810, GET_VAR('sys_role', 3)), (890, GET_VAR('sys_role', 3)), (1999, GET_VAR('sys_role', 3)), (32, GET_VAR('sys_role', 3)), (1000, GET_VAR('sys_role', 3)), (26, GET_VAR('sys_role', 3)), (3000, GET_VAR('sys_role', 3)), (15, GET_VAR('sys_role', 3)), (180, GET_VAR('sys_role', 3)), (300, GET_VAR('sys_role', 3)), (203, GET_VAR('sys_role', 3)), (801, GET_VAR('sys_role', 3)); 
------Journaliste
INSERT INTO sys_role_action ("actionId", "roleId")
	VALUES (510, GET_VAR('sys_role', 4)), (22, GET_VAR('sys_role', 4)), (32, GET_VAR('sys_role', 4)), (3000, GET_VAR('sys_role', 4)), (15, GET_VAR('sys_role', 4)), (180, GET_VAR('sys_role', 4)), (203, GET_VAR('sys_role', 4)); 
------Correspondant
INSERT INTO sys_role_action ("actionId", "roleId")
	VALUES (32, GET_VAR('sys_role', 5)), (3000, GET_VAR('sys_role', 5)), (25, GET_VAR('sys_role', 5)), (27, GET_VAR('sys_role', 5)); 
		
		
--AJOUT sys_thesaurus
INSERT INTO sys_thesaurus(id, name, "pressTitleId", kind, "order", "customerId", "objectClass", "isMandatory", "isUnique", "uniqueCode")
    VALUES (GET_VAR('sys_thesaurus', 0), 'Géographique', GET_VAR('pressTitle', 0), 'edito', 1, GET_VAR('customer', 0), '{article,photo,printCategory}', true, false, GET_VAR('sys_thesaurus', 0)),
			(GET_VAR('sys_thesaurus', 10), 'Éléments permanents', GET_VAR('pressTitle', 0), 'media', 2, GET_VAR('customer', 0), '{permanentElt}', true, true, GET_VAR('sys_thesaurus', 10)),
			(GET_VAR('sys_thesaurus', 20), 'Département AL', GET_VAR('pressTitle', 0), 'edito', 3, GET_VAR('customer', 0), '{annonce}', true, true, GET_VAR('sys_thesaurus', 20)),
			(GET_VAR('sys_thesaurus', 30), 'Rubrique AL', GET_VAR('pressTitle', 0), 'edito', 4, GET_VAR('customer', 0), '{annonce}', true, true, GET_VAR('sys_thesaurus', 30));
SELECT pg_catalog.setval('sys_thesaurus_id_seq', GET_VAR('sys_thesaurus', 50), true);
		
-- AJOUT sys_color
INSERT INTO sys_color (id, "pressTitleId", "displayName", "indesignName", "order")
	VALUES (GET_VAR('sys_color', 0), GET_VAR('pressTitle', 0), 'Couleur Test 1', 'CT1', 1),
		(GET_VAR('sys_color', 10), GET_VAR('pressTitle', 0), 'Couleur Test 2', 'CT2', 2);
SELECT pg_catalog.setval('sys_color_id_seq', GET_VAR('sys_color', 50), true);
	
	
-- AJOUT sys_item_has_rights (Tous les droits pour le groupe admin)
INSERT INTO sys_item_has_rights ("className","objectId","groupId",level)
	VALUES ('pressTitle', GET_VAR('pressTitle', 0), GET_VAR('sys_group', 0), 1),
			('cahier', GET_VAR('cahier', 0), GET_VAR('sys_group', 0), 1),
			('edition', GET_VAR('edition', 0), GET_VAR('sys_group', 0), 1),
			('edition', GET_VAR('edition', 10), GET_VAR('sys_group', 0), 1),
			('sys_color', GET_VAR('sys_color', 0), GET_VAR('sys_group', 0), 1),
			('sys_color', GET_VAR('sys_color', 10), GET_VAR('sys_group', 0), 1);
INSERT INTO sys_item_has_rights ("className","objectId","groupId",level)
	SELECT 'sys_transition', id, GET_VAR('sys_group', 0), 4 FROM sys_transition;
INSERT INTO sys_item_has_rights ("className","objectId","groupId",level)
	SELECT 'sys_state', id, GET_VAR('sys_group', 0), 3 FROM sys_state;










