-- Copyright © 2024, UChicago Argonne, LLC
-- BSD OPEN SOURCE LICENSE. Full license can be found in LICENSE.md
create trigger if not exists network_updated_link before update of link on Link
  begin
    select raise(ABORT, 'You cannot re-number a link.');
  end;

--##

create trigger if not exists network_eliminates_ab_direction after update of lanes_ab on Link
  when
     (new.lanes_ab = 0 AND old.lanes_ab > 0)
  begin
    update link set
    cap_ab = 0,
    fspd_ab = 0
    where link.rowid = new.rowid;

    DELETE FROM Connection WHERE link=new.link AND "dir"=0;
    Delete from Pocket where link=old.link and dir = 0;
    Delete from Sign where link=old.link and dir = 0;
  end;

--##

create trigger if not exists network_eliminates_ba_direction after update of lanes_ba on Link
  when
     (new.lanes_ba = 0 AND old.lanes_ba > 0)
  begin
    update link set
    cap_ba = 0,
    fspd_ba = 0
    where link.rowid = new.rowid;

    Delete from Pocket where link=old.link and dir = 1;
    Delete from Sign where link=old.link and dir = 1;
    DELETE FROM Connection WHERE link=new.link AND "dir"=1;
  end;


--##
create trigger if not exists network_changes_ab_lanes after update of lanes_ab on Link
  begin
    INSERT INTO editing_table VALUES(NULL, 'Link', 'link',new.link, 'lanes_ab', NULL, 'EDIT', 0, '');
  end;

--##
create trigger if not exists network_changes_ba_lanes after update of lanes_ba on Link
  begin
    INSERT INTO editing_table VALUES(NULL, 'Link', 'link',new.link, 'lanes_ba', NULL, 'EDIT', 0, '');
  end;

--##
create trigger if not exists network_fspd_ab_only_positive_for_available_direction after update of fspd_ab on Link
  when
     (new.lanes_ab = 0 and new.fspd_ab != 0)
  begin
        update Link set fspd_ab = 0 where ROWID=new.ROWID;
  end;

--##
create trigger if not exists network_fspd_ba_only_positive_for_available_direction after update of fspd_ba on Link
  when
     (new.lanes_ba = 0 and new.fspd_ba != 0)
  begin
    update Link set fspd_ba = 0 where ROWID=new.ROWID;
  end;

--##
create trigger if not exists network_cap_ab_only_positive_for_available_direction after update of cap_ab on Link
  when
     (new.lanes_ab = 0 and new.cap_ab != 0)
  begin
    update Link set cap_ab = 0 where ROWID=new.ROWID;
  end;

--##
create trigger if not exists network_cap_ba_only_positive_for_available_direction after update of cap_ba on Link
  when
     (new.lanes_ba = 0 and new.cap_ba != 0)
  begin
    update Link set cap_ba = 0 where ROWID=new.ROWID;
  end;

--##
create trigger if not exists network_preserves_cap_ab_direction after update of cap_ab on Link
  when
     (new.lanes_ab > 0 and new.cap_ab <= 0)
  begin
    select raise(ROLLBACK, "You can't have zero capacity for positive number of lanes");
  end;

--##
create trigger if not exists network_preserves_cap_ba_direction after update of cap_ba on Link
  when
     (new.lanes_ba > 0 and new.cap_ba <= 0)
  begin
    select raise(ROLLBACK, "You can't have zero capacity for positive number of lanes");
  end;


--##

create trigger if not exists network_preserves_fspd_ab_direction after update of fspd_ab on Link
  when
     (new.lanes_ab > 0 and new.fspd_ab <= 0)
  begin
    select raise(ROLLBACK, "You can't have zero fspd for positive number of lanes");
  end;

--##

create trigger if not exists network_preserves_fspd_ba_direction after update of fspd_ba on Link
  when
     (new.lanes_ba > 0 and new.fspd_ba <= 0)
  begin
    select raise(ROLLBACK, "You can't have zero fspd for positive number of lanes");
  end;

--##
create trigger if not exists link_queue_edit_delete_link after delete on Link
  begin
    Delete from Location_Links where link=old.link;
    Delete from Pocket where link=old.link;
    Delete from Sign where link=old.link;
    Delete from Connection where link=old.link;
    Delete from Connection where to_link=old.link;
    Delete from Transit_Walk where ref_link=old.link;
    Delete from Transit_Bike where ref_link=old.link;

    INSERT INTO editing_table Values(NULL, 'Link', 'link', old.link, NULL, NULL, 'DELETE', 0, '');
    INSERT INTO editing_table Values(NULL, 'Link', 'link', old.link, 'node_a', old.node_a, 'DELETE', 0, '');
    INSERT INTO editing_table Values(NULL, 'Link', 'link', old.link, 'node_b', old.node_b, 'DELETE', 0, '');
  end;

--##
create trigger if not exists link_queue_edit_add_link after INSERT on Link
  begin
    INSERT INTO editing_table VALUES(NULL, 'Link', 'link', new.link, NULL, NULL, 'ADD', 0, '');
  end;

--##
create trigger if not exists link_queue_edit_change_geo_link after UPDATE of geo on Link
  begin
    INSERT INTO editing_table VALUES(NULL, 'Link', 'link', new.link, 'geo', NULL, 'EDIT', 0, '');
  end;

--##
create trigger if not exists link_queue_edit_change_type_link after UPDATE of "type" on Link
  begin
    INSERT INTO editing_table VALUES(NULL, 'Link', 'link', new.link, 'type', old.type, 'EDIT', 0, '');
  end;


--##
create trigger if not exists node_queue_edit_delete after DELETE on Node
  begin
    INSERT INTO editing_table VALUES(NULL, 'Node', 'node', old.node, NULL, NULL, 'DELETE', 0, '');
    Delete from Signal where nodes=old.node;
  end;

--##
create trigger if not exists network_changes_on_zone_node after update of "zone" on node
begin
    INSERT INTO editing_table VALUES(NULL, 'Node', 'node',new.node, 'zone', NULL, 'EDIT', 0, '');
end;

--##
create trigger if not exists network_update_link_toll_counterpart after update of "link" on Link
begin
    update Link set toll_counterpart = new.link where toll_counterpart=old.link;
end;

--##
create trigger if not exists network_prevent_delete_toll_counterpart before delete on Link
    when (SELECT count(*) FROM link WHERE toll_counterpart = old.link) > 0
  BEGIN
    SELECT raise(ABORT, 'Node cannot delete a link that is the counterpart to a tolled facility');
  END;


--##
-- create trigger if not exists link_delete_toll_for_link_direction after update of "dir" on Link
--   begin
--     delete from Toll_Pricing where  link = new.link and "dir"=old.dir;
--   end;

--##
--create trigger if not exists link_add_to_transit_walk_add_links after INSERT on Link
--  when
--      (Select INSTR((SELECT use_codes from Link_type where link_type=new.type), "WALK"))>0
--  begin
--    INSERT INTO Transit_Walk VALUES(NULL, new.node_a, new.node_b, new.length, new.link, new.geo);
--  end;

--##
--create trigger if not exists link_add_to_transit_walk_change_type after update of "type" on Link
--  when
--      (select sum(q) from (SELECT (Select INSTR((SELECT use_codes from Link_type where link_type=new.type), "WALK")>0) q
--      UNION ALL select (select count(*) from Transit_Walk where ref_link = new.link)=0 q))>1
--  begin
--    INSERT INTO Transit_Walk VALUES(NULL, new.node_a, new.node_b, new.length, new.link, new.geo);
--  end;

