--
--  mocha_instances.sql - defines the mocha_instances table
--
--  Author:
--       Michael Becker <alcexhim@gmail.com>
--
--  Copyright (c) 2021 Mike Becker's Software
--
--  This program is free software: you can redistribute it and/or modify
--  it under the terms of the GNU General Public License as published by
--  the Free Software Foundation, either version 3 of the License, or
--  (at your option) any later version.
--
--  This program is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with this program.  If not, see <http://www.gnu.org/licenses/>.

DROP PROCEDURE IF EXISTS mocha_build_tenant_from_template;

CREATE PROCEDURE mocha_build_tenant_from_template
(
	IN p_tenant_id INT,
	IN p_template_id INT
)
BEGIN

	-- we don't want to use TRUNCATE here since we only want to zap the specified tenant
	CALL mocha_truncate_tenant(p_tenant_id);
	
	CALL mocha_select_tenant(p_tenant_id);
	/*

	-- Class
	CALL mocha_create_class(1, 'B9C9B9B7AD8A4CBDAA6BE05784630B6B', NULL, NULL, @dummy);
	-- Attribute
	CALL mocha_create_class(2, 'F9CD7751EF624F7C8A28EBE90B8F46AA', NULL, NULL, @dummy);
	-- Relationship
	CALL mocha_create_class(3, '9B0A80F9C3254D36997CFB4106204648', NULL, NULL, @dummy);

	-- Class.has Instance
	CALL mocha_create_instance_of(mocha_get_instance_by_global_identifier('9B0A80F9C3254D36997CFB4106204648'), '7EB41D3C2AE9488483A4E59441BCAEFB', NULL, NULL, @dummy)
	-- Instance.for Class
	CALL mocha_create_instance_of(mocha_get_instance_by_global_identifier('9B0A80F9C3254D36997CFB4106204648'), '494D5A6D04BE477B8763E3F57D0DD8C8', NULL, NULL, @dummy)

	CALL mocha_set_parent_class(mocha_get_instance_by_key(1, 1), mocha_get_instance_by_key(1, 1), NULL, NOW());

	-- Text Attribute
	CALL mocha_create_class(4, 'C2F3654260C34B9E9A96CA9B309C43AF', NULL, NULL, @dummy);

	-- Text Attribute: 'Name'
	CALL mocha_create_instance_of(mocha_get_instance_by_key(1, 4), '9153A637992E4712ADF2B03F0D9EDEA6', NULL, NOW(), @dummy);
	CALL mocha_set_attribute_value(mocha_get_instance_by_key(4, 1), mocha_get_instance_by_global_identifier('9153A637992E4712ADF2B03F0D9EDEA6'), 'Name', NULL, NULL);

	-- Text Attribute: 'Value'
	CALL mocha_create_instance_of(mocha_get_instance_by_key(1, 4), '041DD7FD2D9C412B8B9DD7125C166FE0', NULL, NOW(), @dummy);
	CALL mocha_set_attribute_value(mocha_get_instance_by_key(4, 2), mocha_get_instance_by_global_identifier('9153A637992E4712ADF2B03F0D9EDEA6'), 'Value', NULL, NULL);

	-- Text Attribute: 'Verb'
	CALL mocha_create_instance_of(mocha_get_instance_by_key(1, 4), '61345a5d33974a9687978863f03a476c', NULL, NOW(), @dummy);
	CALL mocha_set_attribute_value(mocha_get_instance_by_key(4, 3), mocha_get_instance_by_global_identifier('9153A637992E4712ADF2B03F0D9EDEA6'), 'Verb', NULL, NULL);

	-- Text Attribute: 'User Name'
	CALL mocha_create_instance_of(mocha_get_instance_by_key(1, 4), '960FAF025C5940F791A720012A99D9ED', NULL, NOW(), @dummy);
	CALL mocha_set_attribute_value(mocha_get_instance_by_key(4, 4), mocha_get_instance_by_global_identifier('9153A637992E4712ADF2B03F0D9EDEA6'), 'User Name', NULL, NULL);

	-- Text Attribute: 'Password Hash'
	CALL mocha_create_instance_of(mocha_get_instance_by_key(1, 4), 'F377FC294DF14AFB96434191F37A00A9', NULL, NOW(), @dummy);
	CALL mocha_set_attribute_value(mocha_get_instance_by_key(4, 5), mocha_get_instance_by_global_identifier('9153A637992E4712ADF2B03F0D9EDEA6'), 'Password Hash', NULL, NULL);

	-- Text Attribute: 'Password Salt'
	CALL mocha_create_instance_of(mocha_get_instance_by_key(1, 4), '8C5A99BC40ED4FA2B23FF373C1F3F4BE', NULL, NOW(), @dummy);
	CALL mocha_set_attribute_value(mocha_get_instance_by_key(4, 6), mocha_get_instance_by_global_identifier('9153A637992E4712ADF2B03F0D9EDEA6'), 'Password Salt', NULL, NULL);

	-- Text Attribute: 'Token'
	CALL mocha_create_instance_of(mocha_get_instance_by_key(1, 4), 'da7686b638034f1597f67f8f3ae16668', NULL, NOW(), @dummy);
	CALL mocha_set_attribute_value(mocha_get_instance_by_key(4, 7), mocha_get_instance_by_global_identifier('9153A637992E4712ADF2B03F0D9EDEA6'), 'Token', NULL, NULL);

	-- name = 9153A637992E4712ADF2B03F0D9EDEA6
	-- value = 041DD7FD2D9C412B8B9DD7125C166FE0

	-- CALL mocha_assign_translation(mocha_get_instance_by_global_identifier('61345a5d33974a9687978863f03a476c'), mocha_get_instance_by_global_identifier(''))

	CALL mocha_create_class(39, '9C6871C19A7F4A3A900E69D1D9E24486', NULL, NULL, @dummy);
	CALL mocha_set_attribute_value(mocha_get_instance_by_key(1, 39), mocha_get_instance_by_global_identifier('9153A637992E4712ADF2B03F0D9EDEA6'), 'System User', NULL, NULL);

	CALL mocha_create_class(96, '703F9D65C5844D9FA656D0E3C247FF1F', NULL, NULL, @dummy);
	CALL mocha_set_attribute_value(mocha_get_instance_by_key(1, 96), mocha_get_instance_by_global_identifier('9153A637992E4712ADF2B03F0D9EDEA6'), 'Tenant', NULL, NULL);

	-- zq-environments, 39$1
	CALL mocha_create_instance_of(mocha_get_instance_by_key(1, 39), 'B066A54BB1604510A805436D3F90C2E6', NULL, NOW(), @dummy);
	-- username
	CALL mocha_set_attribute_value(mocha_get_instance_by_key(39, 1), mocha_get_instance_by_global_identifier('960FAF025C5940F791A720012A99D9ED'), 'zq-environments', NULL, NULL);

	-- zq-developer, 39$2
	CALL mocha_create_instance_of(mocha_get_instance_by_key(1, 39), '098DDA82CD044B538C7589D420EA6902', NULL, NOW(), @dummy);
	-- username
	CALL mocha_set_attribute_value(mocha_get_instance_by_key(39, 2), mocha_get_instance_by_global_identifier('960FAF025C5940F791A720012A99D9ED'), 'zq-developer', NULL, NULL);
	-- password
	CALL mocha_set_attribute_value(mocha_get_instance_by_key(39, 2), mocha_get_instance_by_global_identifier('F377FC294DF14AFB96434191F37A00A9'), 'f4f166c4d578cb5ca942e07851d7c09de07d417463f2d8e5165a779f768d14b370cd1e82826a94b617b6c6359253e8c12ea8285cba1e6e69e2e13f2bdc0425d0', NULL, NULL);
	-- salt
	CALL mocha_set_attribute_value(mocha_get_instance_by_key(39, 2), mocha_get_instance_by_global_identifier('8C5A99BC40ED4FA2B23FF373C1F3F4BE'), '7e893ba949b041bab73c6f4f0bcb9413', NULL, NULL);


	CALL mocha_create_user('superuser', '69e291d8381b4ad89013f3b0a0c693fe');
	CALL mocha_create_user('zq-support', '232A8CBF0D2B4BDABE863E2FA25A3FB5');
	CALL mocha_create_user('zq-configurator', 'FB20A79CEAA24A98A1DABDC351854694');
	CALL mocha_create_user('zq-implementer', '63F2EF51DC7348EC856A6FBBEDE01A8A');
	CALL mocha_create_user('admin', '739C26BC740F4CB0BCB12A28FA570E7D');

	-- default tenant
	CALL mocha_create_instance_of(mocha_get_instance_by_key(1, 96), 'F2C9D4A99EFB426384DB66A9DA65AD00', NULL, NOW(), @dummy);

	-- User Login
	CALL mocha_create_class(105, '64F4BCDB38D04373BA308AE99AF1A5F7', NULL, NULL, @dummy);
	CALL mocha_set_attribute_value(mocha_get_instance_by_key(1, 105), mocha_get_instance_by_global_identifier('9153A637992E4712ADF2B03F0D9EDEA6'), 'User Login', NULL, NULL);

	-- relationship : User Login.has User
	CALL mocha_create_instance_of(mocha_get_instance_by_key(1, 3), '85B40E4B849B4006A9C04E201B25975F', NULL, NOW(), @dummy);
	-- relationship : User.for User Login
	CALL mocha_create_instance_of(mocha_get_instance_by_key(1, 3), 'C79A6041FC9441A59860D443C60FA7DE', NULL, NOW(), @dummy);
*/

	CALL mocha_release_tenant();
	
END;
