DROP PROCEDURE IF EXISTS mocha_create_tenant;

CREATE PROCEDURE mocha_create_tenant
(
    IN p_tenant_name VARCHAR(32),
    IN p_global_identifier CHAR(32)
)
BEGIN
    DECLARE z_global_identifier CHAR(32);

    IF p_global_identifier IS NULL THEN
        SET z_global_identifier = mocha_uuid_v4();
    ELSE
        SET z_global_identifier = p_global_identifier;
    END IF;

	IF NOT (SELECT COUNT(tenant_name) FROM mocha_tenants WHERE tenant_name = p_tenant_name) > 0 THEN

    	INSERT INTO mocha_tenants (tenant_name, global_identifier, effective_date)
    	VALUES (p_tenant_name, z_global_identifier, NOW());

	END IF;

END;
