-- --------------------------------------------------------------------------------
-- _120_template Group Routines
-- --------------------------------------------------------------------------------
DELIMITER $$$


create function template_Create(
    type_id_ int unsigned, 
    type_code_ varchar(30) binary,
    node_id_ int unsigned
) returns int unsigned
comment 'template takes ownership on node_id_'
not deterministic modifies sql data
begin
    declare type_id int unsigned default coalesce(type_id_, nodeType_GetByCode(type_code_));
    declare node_type_id int unsigned default (select n.nodeTypeId from nodeT n where n.id = node_id_);
    
    -- FORWARD: nodeLink_ResetLinks2 is defined in _950_extraUtils
    insert templateT(srcNodeTypeId, dstNodeId, dstNodeTypeId)
    values (type_id, nodeLink_ResetLinks2(null, null, node_id_, false), node_type_id);
    
    return type_id;
end$$$


create function template_Delete(
    type_id_ int unsigned, 
    type_code_ varchar(30) binary,
    node_id_ int unsigned
) returns int unsigned
not deterministic modifies sql data
begin
    declare type_id int unsigned default coalesce(type_id_, nodeType_GetByCode(type_code_));
    
    set @1 = nodeLink_UpdateStart();
    
    insert zzzDevNullTemplateT(n)
    select __node_Delete(t.dstNodeId)
    from templateT t
    where t.srcNodeTypeId = type_id
        and t.dstNodeId = coalesce(node_id_, t.dstNodeId);
    
    set @2 = nodeLink_UpdateEnd();

    -- deleted nodes will delete templateT record because of FK constraints
    /*delete from templateT
    where srcNodeTypeId = type_id
        and dstNodeId = coalesce(node_id_, dstNodeId);*/
    
    return 1;
end$$$


create function __template_DuplicateNode(node_id_ int unsigned) returns int unsigned
not deterministic modifies sql data
begin
    declare res int unsigned;
    declare internal_type_id varchar(10) binary;
    
    select nt.internalTypeId 
    into internal_type_id
    from nodeT n 
    join nodeTypeT nt on n.nodeTypeId = nt.id 
    where n.id = node_id_;
    
    case internal_type_id
        when 'tag' then
            set res = __tag_Duplicate(node_id_);
        when 'attribute' then
            set res = __attribute_Duplicate(node_id_);
        else
            call raiseError1('template', 'duplicate', internal_type_id, 'cannot duplicate node of this type');
    end case;
    
    return res;
end$$$


create function template_CreateSubs(node_id_ int unsigned) returns int unsigned
not deterministic modifies sql data
begin
    declare type_id int unsigned default (select n.nodeTypeId from nodeT n where n.id = node_id_);
    
    drop temporary table if exists templateCreateSubsMapping;
    drop temporary table if exists templateCreateSubsMapping2;
    create temporary table templateCreateSubsMapping(
        oldNodeId int unsigned not null primary key,
        newNodeId int unsigned not null
    );
    create temporary table templateCreateSubsMapping2 like templateCreateSubsMapping;
    
    drop temporary table if exists templateCreateSubsLinks;
    create temporary table templateCreateSubsLinks(
        srcNodeId int unsigned,
        dstNodeId int unsigned,
        primary key(srcNodeId, dstNodeId)
    );

    set @1 = nodeLink_UpdateStart();
    
    insert templateCreateSubsMapping(oldNodeId, newNodeId)
    select t.dstNodeId, __template_DuplicateNode(t.dstNodeId)
    from templateT t
    where t.srcNodeTypeId = type_id;
    
    insert templateCreateSubsMapping2(oldNodeId, newNodeId) 
    select oldNodeId, newNodeId from templateCreateSubsMapping;
    
    insert templateCreateSubsLinks(srcNodeId, dstNodeId)
    select tcsm1.newNodeId, tcsm2.newNodeId
    from templateCreateSubsMapping tcsm1
    join nodeLinkTypeT nlt on nlt.isDefault = 'y'
    join nodeLinkT link on nlt.id = link.nodeLinkTypeId 
        and tcsm1.oldNodeId = link.srcNodeId
        and link.metric = 1
    join templateCreateSubsMapping2 tcsm2 on link.dstNodeId = tcsm2.oldNodeId;

    insert zzzDevNullTemplateT(n)
    select nodeLink_Relink(null, node_id_, tcsm.newNodeId)
    from templateCreateSubsMapping tcsm;

    insert zzzDevNullTemplateT(n)
    select nodeLink_Relink(null, tcsl.srcNodeId, tcsl.dstNodeId)
    from templateCreateSubsLinks tcsl;

    set @2 = nodeLink_UpdateEnd();

    drop temporary table templateCreateSubsMapping;
    drop temporary table templateCreateSubsMapping2;
    drop temporary table templateCreateSubsLinks;

    return node_id_;
end$$$

