
----------------------------------------------------
-- 2.5.11.2
-- SQL for this version ends here
----------------------------------------------------

--#12238
INSERT INTO menu_link (name, presenter, action)
VALUES ('reporting_groups',
        'Reporting:Report',
        'groups');

INSERT INTO menu_link (name, presenter, action)
VALUES ('reporting_group_edit',
        'Reporting:Report',
        'editGroup');

INSERT INTO menu_link (name, presenter, action)
VALUES ('reporting_group_detail',
        'Reporting:Report',
        'detail');

insert into menu_section (name, icon, priority)
select 'reporting', null, 100
where not exists(
    select id from menu_section where name = 'reporting'
    );

INSERT INTO menu_item (id_menu_section, id_agenda, id_calendar, name, icon, id_menu_link, priority)
VALUES ( (SELECT ID FROM menu_section WHERE name = 'reporting')
       , null
       , null
       , 'reportingGroups'
       , 'fad fa-object-group'
       , (SELECT ID FROM menu_link WHERE name = 'reporting_groups')
       , '5');

INSERT INTO menu_item_has_menu_link (id_menu_item, id_menu_link)
VALUES ( (select id from menu_item where name = 'reportingGroups')
       , (select id from menu_link where name = 'reporting_groups'));

INSERT INTO menu_item_has_menu_link (id_menu_item, id_menu_link)
VALUES ( (select id from menu_item where name = 'reportingGroups')
       , (select id from menu_link where name = 'reporting_group_edit'));

INSERT INTO menu_item_has_menu_link (id_menu_item, id_menu_link)
VALUES ( (select id from menu_item where name = 'reporting')
       , (select id from menu_link where name = 'reporting_group_detail'));



----------------------------------------------------
-- 2.5.12.0
-- SQL for this version ends here
----------------------------------------------------


----------------------------------------------------
-- 2.5.12.1
-- SQL for this version ends here
----------------------------------------------------


----------------------------------------------------
-- 2.5.12.2
-- SQL for this version ends here
----------------------------------------------------

UPDATE flexii.menu_item SET is_for_admin = true WHERE name = 'admin_lookup_edit';
UPDATE flexii.menu_item SET is_for_admin = true WHERE name = 'admin_ae_enable';
UPDATE flexii.menu_item SET is_for_admin = true WHERE name = 'admin_email';
UPDATE flexii.menu_item SET is_for_admin = true WHERE name = 'admin_user_to_group';
UPDATE flexii.menu_item SET is_for_admin = true WHERE name = 'admin_users';
UPDATE flexii.menu_item SET is_for_admin = true WHERE name = 'admin_user_group';
UPDATE flexii.menu_item SET is_for_admin = true WHERE name = 'admin_user_group_agenda';
UPDATE flexii.menu_item SET is_for_admin = true WHERE name = 'serverInfoAdmin';
UPDATE flexii.menu_item SET is_for_admin = true WHERE name = 'permanent_ae_delete';
UPDATE flexii.menu_item SET is_for_admin = true WHERE name = 'system_process';
UPDATE flexii.menu_item SET is_for_admin = true WHERE name = 'rabbitDisplayAdmin';

----------------------------------------------------
-- 2.5.15.0
-- SQL for this version ends here
----------------------------------------------------

----------------------------------------------------
-- 2.5.15.1
-- SQL for this version ends here
----------------------------------------------------


alter table agenda_segment
    alter column file_source_table set not null;
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- SEGMENT SPRÁVCE TURNAJE
INSERT INTO agenda_segment ("name", source_table, file_source_table) VALUES('tournament_manager', 'agenda_values', 'agenda_has_file');

INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('tournament_manager_main', false, NULL);

INSERT INTO agenda_has_segment_definition (id_agenda, id_agenda_segment, priority, is_main)
VALUES(
          (select id from agenda where name = 'tournaments')
      , (select id from agenda_segment where name = 'tournament_manager')
      , 1, false
      );

INSERT INTO agenda_has_segment_has_attribute_group_definition (id_agenda_has_segment_definition, id_agenda_attribute_group, is_read_only, priority)
VALUES(
          (select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'tournaments')
             and id_agenda_segment = (select id from agenda_segment where name = 'tournament_manager')
          )
      , (select id from agenda_attribute_group where name = 'tournament_manager_main')
      , false, 1
      );

-- Správce turnaje
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('tournament_has_manager', 'Správce turnaje', 0, 'tournament_has_manager', NULL, 1, (select id from attribute_data_type where name = 'appuser'));

/*INSERT INTO agenda_has_agenda (name, id_agenda, id_agenda_linked, relation, is_unique, priority, is_quick_add_link, is_quick_add_link_linked, is_quick_add_agenda, is_quick_add_agenda_linked, is_custom_name)
VALUES (
           'tournament_has_manager'
       , (select id from agenda where name = 'members')
       , (select id from agenda where name = 'tournaments')
       , 2, false, 121, false, false, false, false, true);
*/

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'tournament_has_manager')
      , 20, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL, true, true
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'tournaments')
                                                                                                  and id_agenda_segment = (select id from agenda_segment where name = 'tournament_manager')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'tournament_manager_main')
          )
      );


/*
NEJDŘÍV PŘIDAT LOOKUPY CO NEMAM

*/

-- JEDNOTLVCI / DRŽUSTVA ---
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES ('competition_team_category', 'družstva', 0, 'lookup_competition_team_category', NULL, 1,
        (select id from attribute_data_type where name = 'lookup'));

INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY)
VALUES ('lookup_competition_team_category', 'ct1', 'Jednotlivci', 10);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY)
VALUES ('lookup_competition_team_category', 'ct2', 'Družstva', 20);


INSERT INTO agenda_definition ( id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height
                              , is_in_datagrid, is_search_by, in_title_and_priority
                              , id_agenda_segment_attribute_group_definition)
VALUES ( (select id from attribute where name = 'competition_team_category')
       , 20, true, NULL, false, false, NULL, 3, 0, 1, true, true, null
       , (select id
          from agenda_has_segment_has_attribute_group_definition
          where id_agenda_has_segment_definition = (select id
                                                    from agenda_has_segment_definition
                                                    where id_agenda = (select id from agenda where name = 'competitions')
                                                      and id_agenda_segment =
                                                          (select id from agenda_segment where name = 'competitions_main'))
            and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'competitions_main')));

--------TYP PODKATEGORIE
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES ('competition_type_of_subcategory', 'typ podkategorie', 0, 'lookup_competition_type_of_subcategory', NULL, 1,
        (select id from attribute_data_type where name = 'lookup'));

INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY)
VALUES ('lookup_competition_type_of_subcategory', 'subcategory1', 'PČR', 10);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY)
VALUES ('lookup_competition_type_of_subcategory', 'subcategory2', 'MČR', 20);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY)
VALUES ('lookup_competition_type_of_subcategory', 'subcategory3', 'První liga', 30);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY)
VALUES ('lookup_competition_type_of_subcategory', 'subcategory4', 'Baráž', 30);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY)
VALUES ('lookup_competition_type_of_subcategory', 'subcategory5', 'Ostatní', 40);



INSERT INTO agenda_definition ( id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height
                              , is_in_datagrid, is_search_by, in_title_and_priority
                              , id_agenda_segment_attribute_group_definition)
VALUES ( (select id from attribute where name = 'competition_type_of_subcategory')
       , 40, true, NULL, false, false, NULL, 3, 0, 1, true, true, null
       , (select id
          from agenda_has_segment_has_attribute_group_definition
          where id_agenda_has_segment_definition = (select id
                                                    from agenda_has_segment_definition
                                                    where id_agenda = (select id from agenda where name = 'competitions')
                                                      and id_agenda_segment =
                                                          (select id from agenda_segment where name = 'competitions_main'))
            and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'competitions_main')));
----- TYP MČR
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES ('competition_type_of_championship', 'typ mistrovství', 0, 'lookup_competition_type_of_championship', NULL, 1,
        (select id from attribute_data_type where name = 'lookup'));

INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY)
VALUES ('lookup_competition_type_of_championship', 'championship1', 'Extraliga', 10);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY)
VALUES ('lookup_competition_type_of_championship', 'championship2', 'Dorostenecká liga', 20);

INSERT INTO agenda_definition ( id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height
                              , is_in_datagrid, is_search_by, in_title_and_priority
                              , id_agenda_segment_attribute_group_definition)
VALUES ( (select id from attribute where name = 'competition_type_of_championship')
       , 50, true, NULL, false, false, NULL, 3, 0, 1, true, true, null
       , (select id
          from agenda_has_segment_has_attribute_group_definition
          where id_agenda_has_segment_definition = (select id
                                                    from agenda_has_segment_definition
                                                    where id_agenda = (select id from agenda where name = 'competitions')
                                                      and id_agenda_segment =
                                                          (select id from agenda_segment where name = 'competitions_main'))
            and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'competitions_main')));

-- PODMÍNKY PRO SCHOVÁVÁNÍ
-- TYP PODKATEGORIE MČR UKAŽ TYP MISTROSTVÍ
INSERT INTO agenda_definition_specification_by_value
(id_agenda_definition, id_agenda_definition_control, value_control, is_hidden, is_not_null, default_value, is_disabled,
 is_read_only)
VALUES ((SELECT id
         FROM agenda_definition
         WHERE enabled = true
           AND id_attribute = (SELECT id FROM attribute WHERE name = 'competition_type_of_championship')
           AND id_agenda_segment_attribute_group_definition = (select id
                                                               from agenda_has_segment_has_attribute_group_definition
                                                               where id_agenda_has_segment_definition = (select id
                                                                                                         from agenda_has_segment_definition
                                                                                                         where id_agenda = (select id from agenda where name = 'competitions')
                                                                                                           and id_agenda_segment =
                                                                                                               (select id from agenda_segment where name = 'competitions_main'))
                                                                 and id_agenda_attribute_group =
                                                                     (select id from agenda_attribute_group where name = 'competitions_main')))
           ,
        (SELECT id
         FROM agenda_definition
         WHERE enabled = true
           AND id_attribute = (SELECT id FROM attribute WHERE name = 'competition_type_of_subcategory')
           AND id_agenda_segment_attribute_group_definition = (select id
                                                               from agenda_has_segment_has_attribute_group_definition
                                                               where id_agenda_has_segment_definition = (select id
                                                                                                         from agenda_has_segment_definition
                                                                                                         where id_agenda = (select id from agenda where name = 'competitions')
                                                                                                           and id_agenda_segment =
                                                                                                               (select id from agenda_segment where name = 'competitions_main'))
                                                                 and id_agenda_attribute_group =
                                                                     (select id from agenda_attribute_group where name = 'competitions_main'))),
        (SELECT id
         FROM master_lookup
         WHERE enabled = true
           AND lookup_name = 'lookup_competition_type_of_subcategory'
           AND name = 'subcategory1'), true, false, NULL, false, false);


INSERT INTO agenda_definition_specification_by_value
(id_agenda_definition, id_agenda_definition_control, value_control, is_hidden, is_not_null, default_value, is_disabled,
 is_read_only)
VALUES ((SELECT id
         FROM agenda_definition
         WHERE enabled = true
           AND id_attribute = (SELECT id FROM attribute WHERE name = 'competition_type_of_championship')
           AND id_agenda_segment_attribute_group_definition = (select id
                                                               from agenda_has_segment_has_attribute_group_definition
                                                               where id_agenda_has_segment_definition = (select id
                                                                                                         from agenda_has_segment_definition
                                                                                                         where id_agenda = (select id from agenda where name = 'competitions')
                                                                                                           and id_agenda_segment =
                                                                                                               (select id from agenda_segment where name = 'competitions_main'))
                                                                 and id_agenda_attribute_group =
                                                                     (select id from agenda_attribute_group where name = 'competitions_main')))
           ,
        (SELECT id
         FROM agenda_definition
         WHERE enabled = true
           AND id_attribute = (SELECT id FROM attribute WHERE name = 'competition_type_of_subcategory')
           AND id_agenda_segment_attribute_group_definition = (select id
                                                               from agenda_has_segment_has_attribute_group_definition
                                                               where id_agenda_has_segment_definition = (select id
                                                                                                         from agenda_has_segment_definition
                                                                                                         where id_agenda = (select id from agenda where name = 'competitions')
                                                                                                           and id_agenda_segment =
                                                                                                               (select id from agenda_segment where name = 'competitions_main'))
                                                                 and id_agenda_attribute_group =
                                                                     (select id from agenda_attribute_group where name = 'competitions_main'))),
        (SELECT id
         FROM master_lookup
         WHERE enabled = true
           AND lookup_name = 'lookup_competition_type_of_subcategory'
           AND name = 'subcategory3'), true, false, NULL, false, false);

INSERT INTO agenda_definition_specification_by_value
(id_agenda_definition, id_agenda_definition_control, value_control, is_hidden, is_not_null, default_value, is_disabled,
 is_read_only)
VALUES ((SELECT id
         FROM agenda_definition
         WHERE enabled = true
           AND id_attribute = (SELECT id FROM attribute WHERE name = 'competition_type_of_championship')
           AND id_agenda_segment_attribute_group_definition = (select id
                                                               from agenda_has_segment_has_attribute_group_definition
                                                               where id_agenda_has_segment_definition = (select id
                                                                                                         from agenda_has_segment_definition
                                                                                                         where id_agenda = (select id from agenda where name = 'competitions')
                                                                                                           and id_agenda_segment =
                                                                                                               (select id from agenda_segment where name = 'competitions_main'))
                                                                 and id_agenda_attribute_group =
                                                                     (select id from agenda_attribute_group where name = 'competitions_main')))
           ,
        (SELECT id
         FROM agenda_definition
         WHERE enabled = true
           AND id_attribute = (SELECT id FROM attribute WHERE name = 'competition_type_of_subcategory')
           AND id_agenda_segment_attribute_group_definition = (select id
                                                               from agenda_has_segment_has_attribute_group_definition
                                                               where id_agenda_has_segment_definition = (select id
                                                                                                         from agenda_has_segment_definition
                                                                                                         where id_agenda = (select id from agenda where name = 'competitions')
                                                                                                           and id_agenda_segment =
                                                                                                               (select id from agenda_segment where name = 'competitions_main'))
                                                                 and id_agenda_attribute_group =
                                                                     (select id from agenda_attribute_group where name = 'competitions_main'))),
        (SELECT id
         FROM master_lookup
         WHERE enabled = true
           AND lookup_name = 'lookup_competition_type_of_subcategory'
           AND name = 'subcategory4'), true, false, NULL, false, false);


INSERT INTO agenda_definition_specification_by_value
(id_agenda_definition, id_agenda_definition_control, value_control, is_hidden, is_not_null, default_value, is_disabled,
 is_read_only)
VALUES ((SELECT id
         FROM agenda_definition
         WHERE enabled = true
           AND id_attribute = (SELECT id FROM attribute WHERE name = 'competition_type_of_championship')
           AND id_agenda_segment_attribute_group_definition = (select id
                                                               from agenda_has_segment_has_attribute_group_definition
                                                               where id_agenda_has_segment_definition = (select id
                                                                                                         from agenda_has_segment_definition
                                                                                                         where id_agenda = (select id from agenda where name = 'competitions')
                                                                                                           and id_agenda_segment =
                                                                                                               (select id from agenda_segment where name = 'competitions_main'))
                                                                 and id_agenda_attribute_group =
                                                                     (select id from agenda_attribute_group where name = 'competitions_main')))
           ,
        (SELECT id
         FROM agenda_definition
         WHERE enabled = true
           AND id_attribute = (SELECT id FROM attribute WHERE name = 'competition_type_of_subcategory')
           AND id_agenda_segment_attribute_group_definition = (select id
                                                               from agenda_has_segment_has_attribute_group_definition
                                                               where id_agenda_has_segment_definition = (select id
                                                                                                         from agenda_has_segment_definition
                                                                                                         where id_agenda = (select id from agenda where name = 'competitions')
                                                                                                           and id_agenda_segment =
                                                                                                               (select id from agenda_segment where name = 'competitions_main'))
                                                                 and id_agenda_attribute_group =
                                                                     (select id from agenda_attribute_group where name = 'competitions_main'))),
        (SELECT id
         FROM master_lookup
         WHERE enabled = true
           AND lookup_name = 'lookup_competition_type_of_subcategory'
           AND name = 'subcategory5'), true, false, NULL, false, false);
--- PERMISION BY DEFINITON
INSERT INTO agenda_entity_permission_by_definition
(id_agenda_definition, is_can_read, is_can_write)
VALUES((select id
        from agenda_definition
        where 1=1
          and id_attribute = (select id from attribute where name = 'tournament_has_manager'))
      , true, true);

-- ROLE SPRÁVCE TURNAJE
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('tournament_manager_role', 'Role správce turnaje', 0, 'tournament_manager_role', NULL, 1, (select id from attribute_data_type where name = 'lookup'));

INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES('tournament_manager_role', 'role1', 'Hlavní správce');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES('tournament_manager_role', 'role2', 'Zástupce správce');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES('tournament_manager_role', 'role3', 'Pomocný správce');

----
--  Zvýšené startovné
----
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES ('tournament_increased_starting_fee', 'Zvýšené startovné', 0.01, NULL, NULL, 1,
        (select id from attribute_data_type where name = 'money'));

INSERT INTO agenda_definition ( id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height
                              , is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES ( (select id from attribute where name = 'tournament_increased_starting_fee')
       , 105, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
       , (select id
          from agenda_has_segment_has_attribute_group_definition
          where id_agenda_has_segment_definition = (select id
                                                    from agenda_has_segment_definition
                                                    where id_agenda = (select id from agenda where name = 'tournaments')
                                                      and id_agenda_segment =
                                                          (select id from agenda_segment where name = 'tournaments_main'))
            and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournaments_main')));


INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'tournament_manager_role')
      , 20, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL, true, true
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'tournaments')
                                                                                                  and id_agenda_segment = (select id from agenda_segment where name = 'tournament_manager')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'tournament_manager_main')
          )
      );


----
--  Stav turnaje
----
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES ('tournament_status', 'Stav turnaje', 0, 'lookup_tournament_status', NULL, 1,
        (select id from attribute_data_type where name = 'lookup'));

UPDATE menu_item SET is_for_admin = true WHERE name = 'admin_lookup_edit';
UPDATE menu_item SET is_for_admin = true WHERE name = 'admin_ae_enable';
UPDATE menu_item SET is_for_admin = true WHERE name = 'admin_email';
UPDATE menu_item SET is_for_admin = true WHERE name = 'admin_user_to_group';
UPDATE menu_item SET is_for_admin = true WHERE name = 'admin_users';
UPDATE menu_item SET is_for_admin = true WHERE name = 'admin_user_group';
UPDATE menu_item SET is_for_admin = true WHERE name = 'admin_user_group_agenda';
UPDATE menu_item SET is_for_admin = true WHERE name = 'serverInfoAdmin';
UPDATE menu_item SET is_for_admin = true WHERE name = 'permanent_ae_delete';
UPDATE menu_item SET is_for_admin = true WHERE name = 'system_process';
UPDATE menu_item SET is_for_admin = true WHERE name = 'rabbitDisplayAdmin';


INSERT INTO agenda_action (name, description, class_name)
VALUES ('TournamentPassword', 'Generuj hesla', 'TournamentPasswordAction');

INSERT INTO agenda_button (id_agenda, name, description, icon, class)
VALUES ((SELECT id FROM agenda WHERE name = 'tournaments'), 'TournamentPassword', 'Generuj hesla', 'fal fa-key', 'btn-info');

INSERT INTO agenda_button_has_action (id_agenda_button, id_agenda_action, priority) VALUES ((SELECT id FROM agenda_button WHERE name = 'TournamentPassword'), (SELECT id FROM agenda_action WHERE name = 'TournamentPassword'), 10);

INSERT INTO agenda_action (name, description, class_name, is_via_rabbit)
VALUES ('TournamentStart', 'Spusť turnaj', 'TournamentStartAction', true);

INSERT INTO agenda_button (id_agenda, name, description, icon, class)
VALUES ((SELECT id FROM agenda WHERE name = 'tournaments'), 'TournamentStart', 'Spustit turnaj', 'fal fa-play', 'btn-info');

INSERT INTO agenda_button_has_action (id_agenda_button, id_agenda_action, priority) VALUES ((SELECT id FROM agenda_button WHERE name = 'TournamentStart'), (SELECT id FROM agenda_action WHERE name = 'TournamentStart'), 10);

UPDATE lookup_locations SET description = name WHERE 1=1;

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('access_type', 'Typ přístupu', 0, 'lookup_access_type', '{"id":"id","name":"name"}', 1, (select id from attribute_data_type where name = 'lookup'));

INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_access_type', 'scale', 'Váha', 10);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_access_type', 'tatami', 'Tatami', 20);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_access_type', 'score', 'Scoreboard', 30);

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, WEIGHT, id_attribute_data_type)
VALUES('access_name', 'Název přístupu', 0, 1, (select id from attribute_data_type where name = 'string'));

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, WEIGHT, id_attribute_data_type)
VALUES('access_password', 'Heslo', 0, 1, (select id from attribute_data_type where name = 'string'));

INSERT INTO agenda_segment (name, source_table, file_source_table) VALUES('tournament_access', 'agenda_values', 'agenda_has_file');

INSERT INTO agenda_attribute_group (name, is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('access_passwords', TRUE, NULL);

--agenda_has_segment_definition
INSERT INTO agenda_has_segment_definition (id_agenda, id_agenda_segment, priority, is_main)
VALUES(
          (select id from agenda where name = 'tournaments')
      , (select id from agenda_segment where name = 'tournament_access')
      , 1, FALSE
      );

INSERT INTO agenda_has_segment_has_attribute_group_definition (id_agenda_has_segment_definition, id_agenda_attribute_group, is_read_only, priority)
VALUES(
          (select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'tournaments')
             and id_agenda_segment = (select id from agenda_segment where name = 'tournament_access')
          )
      , (select id from agenda_attribute_group where name = 'access_passwords')
      , TRUE, 20
      );

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'access_type')
      , 10, true, NULL, false, false, NULL, 3, 0, 1, FALSE, FALSE, null
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'tournaments')
                                                                                                  and id_agenda_segment = (select id from agenda_segment where name = 'tournament_access')
         )
           and id_agenda_attribute_group= (select id from agenda_attribute_group where name = 'access_passwords')
          )
      );

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'access_name')
      , 20, true, NULL, false, false, NULL, 3, 0, 1, FALSE, FALSE, null
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'tournaments')
                                                                                                  and id_agenda_segment = (select id from agenda_segment where name = 'tournament_access')
         )
           and id_agenda_attribute_group= (select id from agenda_attribute_group where name = 'access_passwords')
          )
      );

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'access_password')
      , 30, true, NULL, false, false, NULL, 3, 0, 1, FALSE, FALSE, null
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'tournaments')
                                                                                                  and id_agenda_segment = (select id from agenda_segment where name = 'tournament_access')
         )
           and id_agenda_attribute_group= (select id from agenda_attribute_group where name = 'access_passwords')
          )
      );


--Hmotnost
INSERT INTO agenda_has_agenda_definition (id_agenda_has_agenda, id_attribute, priority, is_not_null, is_unique, default_value)
VALUES ( (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_competitions')
       , (SELECT id FROM attribute WHERE name = 'weightcategories'), 25, FALSE, false, NULL);


INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'weightcategories')
      , 25, true, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
             select id from agenda_has_segment_definition
             where id_agenda = (select id from agenda where name = 'tournaments')
               and id_agenda_segment = (select id from agenda_segment where name = 'tournament_category')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_category_main')
          )
      );


DELETE FROM MASTER_LOOKUP WHERE lookup_name = 'lookup_competition_category';

UPDATE ATTRIBUTE SET lookup = 'lookup_categories', id_attribute_data_type = (SELECT id FROM attribute_data_type WHERE name = 'lookup_solo_table') WHERE name = 'competition_category';

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'access_password')
      , 30, false, NULL, true, true, NULL, 3, 0, 1, false, false, NULL, true, true
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'tournaments')
                                                                                                  and id_agenda_segment = (select id from agenda_segment where name = 'tournament_manager')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'tournament_manager_main')
          )
      );

INSERT INTO agenda_action (name, description, class_name)
VALUES ('TournamentManagerPassword', 'Generuj hesla adminům', 'TournamentManagerPassword');

INSERT INTO agenda_button_has_action (id_agenda_button, id_agenda_action, priority) VALUES ((SELECT id FROM agenda_button WHERE name = 'TournamentPassword'), (SELECT id FROM agenda_action WHERE name = 'TournamentManagerPassword'), 20);

UPDATE agenda_attribute_group SET is_group_recurring = true WHERE name = 'tournament_manager_main';

UPDATE master_lookup SET name = 'men' WHERE lookup_name = 'lookup_gender' AND name = 'm';
UPDATE master_lookup SET name = 'women' WHERE lookup_name = 'lookup_gender' AND name = 'f';
UPDATE master_lookup SET name = 'child' WHERE lookup_name = 'lookup_gender' AND name = 'b';

UPDATE agenda_definition
SET in_title_and_priority = NULL
WHERE id = (SELECT id
            FROM mv_agenda_definition
            WHERE agenda_name = 'clubs' AND segment_name = 'clubs_main' AND attribute_group_name = 'clubs_main' AND attribute_name = 'foreign_club');

UPDATE agenda_definition
SET in_title_and_priority = 10
WHERE id = (SELECT id
            FROM mv_agenda_definition
            WHERE agenda_name = 'clubs' AND segment_name = 'clubs_main' AND attribute_group_name = 'clubs_main' AND attribute_name = 'id_club');

/* ------------ AGENDA API SETTINGS ---------------- */
INSERT INTO appuser (username, psswd, password_ldap, secret_key_ldap, is_ldap, is_sync_exchange, is_two_factor, two_factor_secret, fullname, email, id_locale, is_system_user) VALUES ('hajime', '******', null, null, false, false, false, null, 'Hajime API', 'info@evosoft.cz', 1, true);

INSERT INTO external_application (name, enabled, dt_ins, dt_upd, secret, id_appuser) VALUES ('hajime', true, '2023-04-05 09:33:16.653311', '2023-04-05 09:33:16.653311', '16a4d6a4d8a4as4df96s46gs5fgDM5pD9afd', (SELECT id FROM appuser WHERE username = 'hajime'));
INSERT INTO external_application_api_token (id_external_application, token, dt_valid_from, dt_valid_to, id_appuser_created_by, id_appuser_updated_by) VALUES ((select ID from external_application WHERE name = 'hajime'), '6asdf46sa4fd6s4a6fd4sf64s6af46s4f6s4af64asfsafsg64sd4uyjky468ki4', '2022-04-05 09:33:57.000000', null, 1, 1);

INSERT INTO api_agenda_page_attribute (name) VALUES ('name');
INSERT INTO api_agenda_page_attribute (name) VALUES ('type');
INSERT INTO api_agenda_page_attribute (name) VALUES ('desc');
INSERT INTO api_agenda_page_attribute (name) VALUES ('city');
INSERT INTO api_agenda_page_attribute (name) VALUES ('date');
INSERT INTO api_agenda_page_attribute (name) VALUES ('price');
INSERT INTO api_agenda_page_attribute (name) VALUES ('licence');
INSERT INTO api_agenda_page_attribute (name) VALUES ('max_users');
INSERT INTO api_agenda_page_attribute (name) VALUES ('users');
INSERT INTO api_agenda_page_attribute (name) VALUES ('member_csju_id');
INSERT INTO api_agenda_page_attribute (name) VALUES ('gender');
INSERT INTO api_agenda_page_attribute (name) VALUES ('birth_year');
INSERT INTO api_agenda_page_attribute (name) VALUES ('club_csju_id');
INSERT INTO api_agenda_page_attribute (name) VALUES ('club_name');
INSERT INTO api_agenda_page_attribute (name) VALUES ('surname');
INSERT INTO api_agenda_page_attribute (name) VALUES ('firstname');
INSERT INTO api_agenda_page_attribute (name) VALUES ('birthdate');
INSERT INTO api_agenda_page_attribute (name) VALUES ('short_name');
INSERT INTO api_agenda_page_attribute (name) VALUES ('location');
INSERT INTO api_agenda_page_attribute (name) VALUES ('country');

INSERT INTO api_agenda_page_template (name) VALUES ('hajime_member');
INSERT INTO api_agenda_page_template (name) VALUES ('hajime_club');

refresh materialized view mv_agenda_definition;

INSERT INTO api_agenda_definition_publish (id_external_application, id_agenda_definition) VALUES ((select ID from external_application WHERE name = 'hajime'), (SELECT /* 1 */ id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'members_main' AND attribute_group_name = 'members_main' AND attribute_name = 'id_member'));
INSERT INTO api_agenda_definition_publish (id_external_application, id_agenda_definition) VALUES ((select ID from external_application WHERE name = 'hajime'), (SELECT /* 4 */ id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'members_main' AND attribute_group_name = 'members_main' AND attribute_name = 'firstname'));
INSERT INTO api_agenda_definition_publish (id_external_application, id_agenda_definition) VALUES ((select ID from external_application WHERE name = 'hajime'), (SELECT /* 5 */ id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'members_main' AND attribute_group_name = 'members_main' AND attribute_name = 'surname'));
INSERT INTO api_agenda_definition_publish (id_external_application, id_agenda_definition) VALUES ((select ID from external_application WHERE name = 'hajime'), (SELECT /* 7 */ id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'members_main' AND attribute_group_name = 'members_main' AND attribute_name = 'dt_birthyear'));
INSERT INTO api_agenda_definition_publish (id_external_application, id_agenda_definition) VALUES ((select ID from external_application WHERE name = 'hajime'), (SELECT /* 3 */ id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'members_main' AND attribute_group_name = 'members_main' AND attribute_name = 'gender'));
INSERT INTO api_agenda_definition_publish (id_external_application, id_agenda_definition) VALUES ((select ID from external_application WHERE name = 'hajime'), (SELECT /* 6 */ id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'members_main' AND attribute_group_name = 'members_main' AND attribute_name = 'dt_birthdate'));
INSERT INTO api_agenda_definition_publish (id_external_application, id_agenda_definition) VALUES ((select ID from external_application WHERE name = 'hajime'), (SELECT /* 70 */ id FROM mv_agenda_definition WHERE agenda_name = 'clubs' AND segment_name = 'clubs_main' AND attribute_group_name = 'clubs_main' AND attribute_name = 'name'));
INSERT INTO api_agenda_definition_publish (id_external_application, id_agenda_definition) VALUES ((select ID from external_application WHERE name = 'hajime'), (SELECT /* 71 */ id FROM mv_agenda_definition WHERE agenda_name = 'clubs' AND segment_name = 'clubs_main' AND attribute_group_name = 'clubs_main' AND attribute_name = 'short_name'));
INSERT INTO api_agenda_definition_publish (id_external_application, id_agenda_definition) VALUES ((select ID from external_application WHERE name = 'hajime'), (SELECT /* 68 */ id FROM mv_agenda_definition WHERE agenda_name = 'clubs' AND segment_name = 'clubs_main' AND attribute_group_name = 'clubs_main' AND attribute_name = 'id_club'));
INSERT INTO api_agenda_definition_publish (id_external_application, id_agenda_definition) VALUES ((select ID from external_application WHERE name = 'hajime'), (SELECT /* 75 */ id FROM mv_agenda_definition WHERE agenda_name = 'clubs' AND segment_name = 'clubs_main' AND attribute_group_name = 'clubs_main' AND attribute_name = 'locations'));
INSERT INTO api_agenda_definition_publish (id_external_application, id_agenda_definition) VALUES ((select ID from external_application WHERE name = 'hajime'), (SELECT /* 100 */ id FROM mv_agenda_definition WHERE agenda_name = 'clubs' AND segment_name = 'clubs_address' AND attribute_group_name = 'clubs_address_main' AND attribute_name = 'country'));
INSERT INTO api_agenda_definition_publish (id_external_application, id_agenda_definition) VALUES ((select ID from external_application WHERE name = 'hajime'), (SELECT /* 89 */ id FROM mv_agenda_definition WHERE agenda_name = 'clubs' AND segment_name = 'clubs_address' AND attribute_group_name = 'clubs_address_main' AND attribute_name = 'city'));

INSERT INTO api_agenda_page_template_has_page_attribute (id_api_agenda_page_template, id_api_agenda_page_attribute) VALUES ((SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member'), (SELECT /* 10 */ id FROM api_agenda_page_attribute WHERE name = 'member_csju_id'));
INSERT INTO api_agenda_page_template_has_page_attribute (id_api_agenda_page_template, id_api_agenda_page_attribute) VALUES ((SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member'), (SELECT /* 11 */ id FROM api_agenda_page_attribute WHERE name = 'surname'));
INSERT INTO api_agenda_page_template_has_page_attribute (id_api_agenda_page_template, id_api_agenda_page_attribute) VALUES ((SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member'), (SELECT /* 12 */ id FROM api_agenda_page_attribute WHERE name = 'gender'));
INSERT INTO api_agenda_page_template_has_page_attribute (id_api_agenda_page_template, id_api_agenda_page_attribute) VALUES ((SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member'), (SELECT /* 13 */ id FROM api_agenda_page_attribute WHERE name = 'birth_year'));
INSERT INTO api_agenda_page_template_has_page_attribute (id_api_agenda_page_template, id_api_agenda_page_attribute) VALUES ((SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member'), (SELECT /* 14 */ id FROM api_agenda_page_attribute WHERE name = 'club_csju_id'));
INSERT INTO api_agenda_page_template_has_page_attribute (id_api_agenda_page_template, id_api_agenda_page_attribute) VALUES ((SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member'), (SELECT /* 15 */ id FROM api_agenda_page_attribute WHERE name = 'club_name'));
INSERT INTO api_agenda_page_template_has_page_attribute (id_api_agenda_page_template, id_api_agenda_page_attribute) VALUES ((SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member'), (SELECT /* 16 */ id FROM api_agenda_page_attribute WHERE name = 'firstname'));
INSERT INTO api_agenda_page_template_has_page_attribute (id_api_agenda_page_template, id_api_agenda_page_attribute) VALUES ((SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member'), (SELECT /* 18 */ id FROM api_agenda_page_attribute WHERE name = 'birthdate'));
INSERT INTO api_agenda_page_template_has_page_attribute (id_api_agenda_page_template, id_api_agenda_page_attribute) VALUES ((SELECT id FROM api_agenda_page_template WHERE name = 'hajime_club'), (SELECT /* 1 */ id FROM api_agenda_page_attribute WHERE name = 'name'));
INSERT INTO api_agenda_page_template_has_page_attribute (id_api_agenda_page_template, id_api_agenda_page_attribute) VALUES ((SELECT id FROM api_agenda_page_template WHERE name = 'hajime_club'), (SELECT /* 4 */ id FROM api_agenda_page_attribute WHERE name = 'city'));
INSERT INTO api_agenda_page_template_has_page_attribute (id_api_agenda_page_template, id_api_agenda_page_attribute) VALUES ((SELECT id FROM api_agenda_page_template WHERE name = 'hajime_club'), (SELECT /* 14 */ id FROM api_agenda_page_attribute WHERE name = 'club_csju_id'));
INSERT INTO api_agenda_page_template_has_page_attribute (id_api_agenda_page_template, id_api_agenda_page_attribute) VALUES ((SELECT id FROM api_agenda_page_template WHERE name = 'hajime_club'), (SELECT /* 19 */ id FROM api_agenda_page_attribute WHERE name = 'short_name'));
INSERT INTO api_agenda_page_template_has_page_attribute (id_api_agenda_page_template, id_api_agenda_page_attribute) VALUES ((SELECT id FROM api_agenda_page_template WHERE name = 'hajime_club'), (SELECT /* 20 */ id FROM api_agenda_page_attribute WHERE name = 'location'));
INSERT INTO api_agenda_page_template_has_page_attribute (id_api_agenda_page_template, id_api_agenda_page_attribute) VALUES ((SELECT id FROM api_agenda_page_template WHERE name = 'hajime_club'), (SELECT /* 21 */ id FROM api_agenda_page_attribute WHERE name = 'country'));


INSERT INTO api_agenda_page_attribute_mapping (id_api_agenda_page_template_has_page_attribute, id_api_agenda_definition_publish) VALUES ((SELECT /* 18 */ id FROM api_agenda_page_template_has_page_attribute WHERE id_api_agenda_page_template = (SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member') AND id_api_agenda_page_attribute = (SELECT /* 10 */ id FROM api_agenda_page_attribute WHERE name = 'member_csju_id')), (SELECT /* 1 */ id FROM api_agenda_definition_publish WHERE id_external_application = (select ID from external_application WHERE name = 'hajime') AND id_agenda_definition = (SELECT /* 1 */ id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'members_main' AND attribute_group_name = 'members_main' AND attribute_name = 'id_member')));
INSERT INTO api_agenda_page_attribute_mapping (id_api_agenda_page_template_has_page_attribute, id_api_agenda_definition_publish) VALUES ((SELECT /* 19 */ id FROM api_agenda_page_template_has_page_attribute WHERE id_api_agenda_page_template = (SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member') AND id_api_agenda_page_attribute = (SELECT /* 11 */ id FROM api_agenda_page_attribute WHERE name = 'surname')), (SELECT /* 2 */ id FROM api_agenda_definition_publish WHERE id_external_application = (select ID from external_application WHERE name = 'hajime') AND id_agenda_definition = (SELECT /* 4 */ id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'members_main' AND attribute_group_name = 'members_main' AND attribute_name = 'surname')));
INSERT INTO api_agenda_page_attribute_mapping (id_api_agenda_page_template_has_page_attribute, id_api_agenda_definition_publish) VALUES ((SELECT /* 20 */ id FROM api_agenda_page_template_has_page_attribute WHERE id_api_agenda_page_template = (SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member') AND id_api_agenda_page_attribute = (SELECT /* 12 */ id FROM api_agenda_page_attribute WHERE name = 'gender')), (SELECT /* 6 */ id FROM api_agenda_definition_publish WHERE id_external_application = (select ID from external_application WHERE name = 'hajime') AND id_agenda_definition = (SELECT /* 3 */ id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'members_main' AND attribute_group_name = 'members_main' AND attribute_name = 'gender')));
INSERT INTO api_agenda_page_attribute_mapping (id_api_agenda_page_template_has_page_attribute, id_api_agenda_definition_publish) VALUES ((SELECT /* 21 */ id FROM api_agenda_page_template_has_page_attribute WHERE id_api_agenda_page_template = (SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member') AND id_api_agenda_page_attribute = (SELECT /* 13 */ id FROM api_agenda_page_attribute WHERE name = 'birth_year')), (SELECT /* 4 */ id FROM api_agenda_definition_publish WHERE id_external_application = (select ID from external_application WHERE name = 'hajime') AND id_agenda_definition = (SELECT /* 7 */ id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'members_main' AND attribute_group_name = 'members_main' AND attribute_name = 'dt_birthyear')));
INSERT INTO api_agenda_page_attribute_mapping (id_api_agenda_page_template_has_page_attribute, id_api_agenda_definition_publish) VALUES ((SELECT /* 24 */ id FROM api_agenda_page_template_has_page_attribute WHERE id_api_agenda_page_template = (SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member') AND id_api_agenda_page_attribute = (SELECT /* 16 */ id FROM api_agenda_page_attribute WHERE name = 'firstname')), (SELECT /* 3 */ id FROM api_agenda_definition_publish WHERE id_external_application = (select ID from external_application WHERE name = 'hajime') AND id_agenda_definition = (SELECT /* 5 */ id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'members_main' AND attribute_group_name = 'members_main' AND attribute_name = 'firstname')));
INSERT INTO api_agenda_page_attribute_mapping (id_api_agenda_page_template_has_page_attribute, id_api_agenda_definition_publish) VALUES ((SELECT /* 25 */ id FROM api_agenda_page_template_has_page_attribute WHERE id_api_agenda_page_template = (SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member') AND id_api_agenda_page_attribute = (SELECT /* 18 */ id FROM api_agenda_page_attribute WHERE name = 'birthdate')), (SELECT /* 8 */ id FROM api_agenda_definition_publish WHERE id_external_application = (select ID from external_application WHERE name = 'hajime') AND id_agenda_definition = (SELECT /* 6 */ id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'members_main' AND attribute_group_name = 'members_main' AND attribute_name = 'dt_birthdate')));
INSERT INTO api_agenda_page_attribute_mapping (id_api_agenda_page_template_has_page_attribute, id_api_agenda_definition_publish) VALUES ((SELECT /* 26 */ id FROM api_agenda_page_template_has_page_attribute WHERE id_api_agenda_page_template = (SELECT id FROM api_agenda_page_template WHERE name = 'hajime_club') AND id_api_agenda_page_attribute = (SELECT /* 1 */ id FROM api_agenda_page_attribute WHERE name = 'name')), (SELECT /* 9 */ id FROM api_agenda_definition_publish WHERE id_external_application = (select ID from external_application WHERE name = 'hajime') AND id_agenda_definition = (SELECT /* 70 */ id FROM mv_agenda_definition WHERE agenda_name = 'clubs' AND segment_name = 'clubs_main' AND attribute_group_name = 'clubs_main' AND attribute_name = 'name')));
INSERT INTO api_agenda_page_attribute_mapping (id_api_agenda_page_template_has_page_attribute, id_api_agenda_definition_publish) VALUES ((SELECT /* 27 */ id FROM api_agenda_page_template_has_page_attribute WHERE id_api_agenda_page_template = (SELECT id FROM api_agenda_page_template WHERE name = 'hajime_club') AND id_api_agenda_page_attribute = (SELECT /* 4 */ id FROM api_agenda_page_attribute WHERE name = 'city')), (SELECT /* 14 */ id FROM api_agenda_definition_publish WHERE id_external_application = (select ID from external_application WHERE name = 'hajime') AND id_agenda_definition = (SELECT /* 89 */ id FROM mv_agenda_definition WHERE agenda_name = 'clubs' AND segment_name = 'clubs_address' AND attribute_group_name = 'clubs_address_main' AND attribute_name = 'city')));
INSERT INTO api_agenda_page_attribute_mapping (id_api_agenda_page_template_has_page_attribute, id_api_agenda_definition_publish) VALUES ((SELECT /* 28 */ id FROM api_agenda_page_template_has_page_attribute WHERE id_api_agenda_page_template = (SELECT id FROM api_agenda_page_template WHERE name = 'hajime_club') AND id_api_agenda_page_attribute = (SELECT /* 14 */ id FROM api_agenda_page_attribute WHERE name = 'club_csju_id')), (SELECT /* 11 */ id FROM api_agenda_definition_publish WHERE id_external_application = (select ID from external_application WHERE name = 'hajime') AND id_agenda_definition = (SELECT /* 68 */ id FROM mv_agenda_definition WHERE agenda_name = 'clubs' AND segment_name = 'clubs_main' AND attribute_group_name = 'clubs_main' AND attribute_name = 'id_club')));
INSERT INTO api_agenda_page_attribute_mapping (id_api_agenda_page_template_has_page_attribute, id_api_agenda_definition_publish) VALUES ((SELECT /* 29 */ id FROM api_agenda_page_template_has_page_attribute WHERE id_api_agenda_page_template = (SELECT id FROM api_agenda_page_template WHERE name = 'hajime_club') AND id_api_agenda_page_attribute = (SELECT /* 19 */ id FROM api_agenda_page_attribute WHERE name = 'short_name')), (SELECT /* 10 */ id FROM api_agenda_definition_publish WHERE id_external_application = (select ID from external_application WHERE name = 'hajime') AND id_agenda_definition = (SELECT /* 71 */ id FROM mv_agenda_definition WHERE agenda_name = 'clubs' AND segment_name = 'clubs_main' AND attribute_group_name = 'clubs_main' AND attribute_name = 'short_name')));
INSERT INTO api_agenda_page_attribute_mapping (id_api_agenda_page_template_has_page_attribute, id_api_agenda_definition_publish) VALUES ((SELECT /* 30 */ id FROM api_agenda_page_template_has_page_attribute WHERE id_api_agenda_page_template = (SELECT id FROM api_agenda_page_template WHERE name = 'hajime_club') AND id_api_agenda_page_attribute = (SELECT /* 20 */ id FROM api_agenda_page_attribute WHERE name = 'location')), (SELECT /* 12 */ id FROM api_agenda_definition_publish WHERE id_external_application = (select ID from external_application WHERE name = 'hajime') AND id_agenda_definition = (SELECT /* 75 */ id FROM mv_agenda_definition WHERE agenda_name = 'clubs' AND segment_name = 'clubs_main' AND attribute_group_name = 'clubs_main' AND attribute_name = 'locations')));
INSERT INTO api_agenda_page_attribute_mapping (id_api_agenda_page_template_has_page_attribute, id_api_agenda_definition_publish) VALUES ((SELECT /* 31 */ id FROM api_agenda_page_template_has_page_attribute WHERE id_api_agenda_page_template = (SELECT id FROM api_agenda_page_template WHERE name = 'hajime_club') AND id_api_agenda_page_attribute = (SELECT /* 21 */ id FROM api_agenda_page_attribute WHERE name = 'country')), (SELECT /* 13 */ id FROM api_agenda_definition_publish WHERE id_external_application = (select ID from external_application WHERE name = 'hajime') AND id_agenda_definition = (SELECT /* 100 */ id FROM mv_agenda_definition WHERE agenda_name = 'clubs' AND segment_name = 'clubs_address' AND attribute_group_name = 'clubs_address_main' AND attribute_name = 'country')));

INSERT INTO api_agenda_agenda_has_agenda (id_external_application, id_agenda_has_agenda, is_creatable, is_deletable, id_appuser_created_by, id_appuser_updated_by) VALUES ((select ID from external_application WHERE name = 'hajime'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), false, false, 1, 1);
INSERT INTO api_agenda_agenda_has_agenda_definition (id_api_agenda_agenda_has_agenda, id_agenda_has_agenda_definition, is_editable, is_required, id_appuser_created_by, id_appuser_updated_by) VALUES ((SELECT id FROM api_agenda_agenda_has_agenda WHERE id_external_application = (select ID from external_application WHERE name = 'hajime') AND id_agenda_has_agenda = (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members')), (SELECT id FROM agenda_has_agenda_definition WHERE id_agenda_has_agenda = (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members') AND id_attribute = (SELECT id FROM attribute WHERE name = 'dt_from')), false, false, 1, 1);
INSERT INTO api_agenda_agenda_has_agenda_definition (id_api_agenda_agenda_has_agenda, id_agenda_has_agenda_definition, is_editable, is_required, id_appuser_created_by, id_appuser_updated_by) VALUES ((SELECT id FROM api_agenda_agenda_has_agenda WHERE id_external_application = (select ID from external_application WHERE name = 'hajime') AND id_agenda_has_agenda = (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members')), (SELECT id FROM agenda_has_agenda_definition WHERE id_agenda_has_agenda = (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members') AND id_attribute = (SELECT id FROM attribute WHERE name = 'dt_to')), false, false, 1, 1);
/* -------------------------------------- */

UPDATE agenda_definition
SET is_not_null = true
WHERE id = (SELECT id
            FROM mv_agenda_definition
            WHERE agenda_name = 'tournaments' AND segment_name = 'tournaments_main' AND attribute_group_name = 'tournaments_address_main' AND attribute_name = 'city');

UPDATE agenda_definition
SET is_not_null = true
WHERE id = (SELECT id
            FROM mv_agenda_definition
            WHERE agenda_name = 'tournaments' AND segment_name = 'tournaments_main' AND attribute_group_name = 'tournaments_address_main' AND attribute_name = 'postal_code');

UPDATE agenda_definition
SET is_not_null = true
WHERE id = (SELECT id
            FROM mv_agenda_definition
            WHERE agenda_name = 'tournaments' AND segment_name = 'tournaments_main' AND attribute_group_name = 'tournaments_address_main' AND attribute_name = 'address_region');

-- #12624

INSERT INTO base.user_group (name, description) VALUES ('regional_union_representatives', 'Zástupci vedení krajský svazů');

INSERT INTO user_group_has_agenda (id_user_group, id_agenda, is_can_read, is_can_write)
values ((select id from base.user_group where name = 'regional_union_representatives'), (select id from agenda where name = 'tournaments'), true, true);

----------- #13419 -----------

UPDATE agenda_definition SET pattern = '^[1-8]{1}'
WHERE id = ( SELECT id
             FROM mv_agenda_definition
             WHERE attribute_name = 'tatami_count'
               AND attribute_group_name = 'tournaments_main'
               AND agenda_name = 'tournaments'
               AND segment_name = 'tournaments_main'
);

UPDATE agenda_definition SET pattern = '^[1-8]{1}'
WHERE id = ( SELECT id
             FROM mv_agenda_definition
             WHERE attribute_name = 'scale_count'
               AND attribute_group_name = 'tournaments_main'
               AND agenda_name = 'tournaments'
               AND segment_name = 'tournaments_main'
);


-- #13438

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('club_hajime_acronym', 'Zkratka klubu - Hajime', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'string'));

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value
                              , is_disabled, is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by,max_length, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'club_hajime_acronym')
      , 45, true, NULL, false, false, NULL, 3, 0, 1, false, false, 12
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'clubs')
                                                                                                  and id_agenda_segment = (select id from agenda_segment where name = 'clubs_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'clubs_main')
          )
      );

INSERT INTO api_agenda_page_attribute (name) VALUES ('abbreviation');

refresh materialized view mv_agenda_definition;

INSERT INTO api_agenda_definition_publish (id_external_application, id_agenda_definition) VALUES ((select ID from external_application WHERE name = 'hajime'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'clubs' AND segment_name = 'clubs_main' AND attribute_group_name = 'clubs_main' AND attribute_name = 'club_hajime_acronym'));
INSERT INTO api_agenda_page_template_has_page_attribute (id_api_agenda_page_template, id_api_agenda_page_attribute) VALUES ((SELECT id FROM api_agenda_page_template WHERE name = 'hajime_club'), (SELECT id FROM api_agenda_page_attribute WHERE name = 'abbreviation'));
INSERT INTO api_agenda_page_attribute_mapping (id_api_agenda_page_template_has_page_attribute, id_api_agenda_definition_publish) VALUES ((SELECT id FROM api_agenda_page_template_has_page_attribute WHERE id_api_agenda_page_template = (SELECT id FROM api_agenda_page_template WHERE name = 'hajime_club') AND id_api_agenda_page_attribute = (SELECT id FROM api_agenda_page_attribute WHERE name = 'abbreviation')), (SELECT id FROM api_agenda_definition_publish WHERE id_external_application = (select ID from external_application WHERE name = 'hajime') AND id_agenda_definition = (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'clubs' AND segment_name = 'clubs_main' AND attribute_group_name = 'clubs_main' AND attribute_name = 'club_hajime_acronym')));

/* -------------------------------------- */

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value
                              , is_disabled, is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by,max_length, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'clubs_has_members')
      , 77, true, NULL, false, false, NULL, 3, 0, 1, false, false, null
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'members')
                                                                                                  and id_agenda_segment = (select id from agenda_segment where name = 'members_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'members_main')
          )
      );

UPDATE agenda_has_agenda SET relation = 5, is_move_primary_on_change = true WHERE name = 'clubs_has_members';

INSERT INTO agenda_action (name, description, class_name, is_via_rabbit) VALUES ('TournamentValidation', 'Validuj turnaj', 'TournamentValidation', true);
INSERT INTO agenda_button_has_action (id_agenda_button, id_agenda_action, priority) VALUES ((SELECT id FROM agenda_button WHERE name = 'TournamentStart'), (SELECT id FROM agenda_action WHERE name = 'TournamentValidation'), 5);


-- #13399
INSERT INTO api_agenda_page_attribute (name) VALUES ('kyu');
INSERT INTO api_agenda_page_attribute (name) VALUES ('dan');

refresh materialized view mv_agenda_definition;

INSERT INTO api_agenda_definition_publish (id_external_application, id_agenda_definition) VALUES ((select ID from external_application WHERE name = 'hajime'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'kyu' AND attribute_group_name = 'kyu' AND attribute_name = 'kyu_level'));
INSERT INTO api_agenda_definition_publish (id_external_application, id_agenda_definition) VALUES ((select ID from external_application WHERE name = 'hajime'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'dan' AND attribute_group_name = 'dan' AND attribute_name = 'dan_level'));
INSERT INTO api_agenda_page_template_has_page_attribute (id_api_agenda_page_template, id_api_agenda_page_attribute) VALUES ((SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member'), (SELECT id FROM api_agenda_page_attribute WHERE name = 'kyu'));
INSERT INTO api_agenda_page_template_has_page_attribute (id_api_agenda_page_template, id_api_agenda_page_attribute) VALUES ((SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member'), (SELECT id FROM api_agenda_page_attribute WHERE name = 'dan'));
INSERT INTO api_agenda_page_attribute_mapping (id_api_agenda_page_template_has_page_attribute, id_api_agenda_definition_publish) VALUES ((SELECT id FROM api_agenda_page_template_has_page_attribute WHERE id_api_agenda_page_template = (SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member') AND id_api_agenda_page_attribute = (SELECT id FROM api_agenda_page_attribute WHERE name = 'kyu')), (SELECT id FROM api_agenda_definition_publish WHERE id_external_application = (select ID from external_application WHERE name = 'hajime') AND id_agenda_definition = (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'kyu' AND attribute_group_name = 'kyu' AND attribute_name = 'kyu_level')));
INSERT INTO api_agenda_page_attribute_mapping (id_api_agenda_page_template_has_page_attribute, id_api_agenda_definition_publish) VALUES ((SELECT id FROM api_agenda_page_template_has_page_attribute WHERE id_api_agenda_page_template = (SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member') AND id_api_agenda_page_attribute = (SELECT id FROM api_agenda_page_attribute WHERE name = 'dan')), (SELECT id FROM api_agenda_definition_publish WHERE id_external_application = (select ID from external_application WHERE name = 'hajime') AND id_agenda_definition = (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'dan' AND attribute_group_name = 'dan' AND attribute_name = 'dan_level')));

-- #13399
INSERT INTO api_agenda_page_attribute (name) VALUES ('dt_birthyear');

INSERT INTO api_agenda_page_template_has_page_attribute (id_api_agenda_page_template, id_api_agenda_page_attribute) VALUES ((SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member'), (SELECT id FROM api_agenda_page_attribute WHERE name = 'dt_birthyear'));
INSERT INTO api_agenda_page_attribute_mapping (id_api_agenda_page_template_has_page_attribute, id_api_agenda_definition_publish) VALUES ((SELECT id FROM api_agenda_page_template_has_page_attribute WHERE id_api_agenda_page_template = (SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member') AND id_api_agenda_page_attribute = (SELECT id FROM api_agenda_page_attribute WHERE name = 'dt_birthyear')), (SELECT id FROM api_agenda_definition_publish WHERE id_external_application = (select ID from external_application WHERE name = 'hajime') AND id_agenda_definition = (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'members_main' AND attribute_group_name = 'members_main' AND attribute_name = 'dt_birthyear')));

UPDATE agenda_definition SET is_not_null = true
WHERE id = ( SELECT id
             FROM mv_agenda_definition
             WHERE attribute_name = 'dt_birthyear'
               AND attribute_group_name = 'members_main'
               AND segment_name = 'members_main'
               AND agenda_name = 'members'
);

UPDATE agenda_definition SET is_not_null = true
WHERE id = ( SELECT id
             FROM mv_agenda_definition
             WHERE attribute_name = 'locations'
               AND attribute_group_name = 'tournaments_main'
               AND segment_name = 'tournaments_main'
               AND agenda_name = 'tournaments'
);

UPDATE agenda_definition SET is_not_null = true
WHERE id = ( SELECT id
             FROM mv_agenda_definition
             WHERE attribute_name = 'kyu_level'
               AND attribute_group_name = 'kyu'
               AND segment_name = 'kyu'
               AND agenda_name = 'members'
);

UPDATE agenda_definition SET is_not_null = true
WHERE id = ( SELECT id
             FROM mv_agenda_definition
             WHERE attribute_name = 'dt_granted'
               AND attribute_group_name = 'kyu'
               AND segment_name = 'kyu'
               AND agenda_name = 'members'
);

UPDATE agenda_definition SET is_not_null = true
WHERE id = ( SELECT id
             FROM mv_agenda_definition
             WHERE attribute_name = 'dan_level'
               AND attribute_group_name = 'dan'
               AND segment_name = 'dan'
               AND agenda_name = 'members'
);

UPDATE agenda_definition SET is_not_null = true
WHERE id = ( SELECT id
             FROM mv_agenda_definition
             WHERE attribute_name = 'dt_granted'
               AND attribute_group_name = 'dan'
               AND segment_name = 'dan'
               AND agenda_name = 'members'
);

UPDATE agenda_definition SET is_search_by = false
WHERE id = ( SELECT id
             FROM mv_agenda_definition
             WHERE attribute_name = 'short_name'
               AND attribute_group_name = 'clubs_main'
               AND segment_name = 'clubs_main'
               AND agenda_name = 'clubs'
);

UPDATE agenda_definition SET is_search_by = false
WHERE id = ( SELECT id
             FROM mv_agenda_definition
             WHERE attribute_name = 'foreign_club'
               AND attribute_group_name = 'clubs_main'
               AND segment_name = 'clubs_main'
               AND agenda_name = 'clubs'
);

UPDATE agenda_has_agenda SET  is_unique = false
where name = 'clubs_has_members';


UPDATE agenda_definition SET is_unique = false
WHERE id IN (SELECT id
             FROM mv_agenda_definition
             WHERE  attribute_group_name = 'tournament_manager_main'
               AND segment_name = 'tournament_manager'
               AND agenda_name = 'tournaments'
);

-- ------ #13555 ------
-- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- --Soutěžní kategorie
-- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--
-- --- Attribute ---
-- --Název váhové kategorie
-- INSERT INTO ATTRIBUTE
-- (NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
-- VALUES('weight_name', 'Název váhové kategorie', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'string'));
--
-- --Váha od
-- INSERT INTO ATTRIBUTE
-- (NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
-- VALUES('weight_from', 'Váha od', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'int'));
--
-- --Váha od
-- INSERT INTO ATTRIBUTE
-- (NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
-- VALUES('weight_to', 'Váha do', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'int'));
--
-- --Věk od
-- INSERT INTO ATTRIBUTE
-- (NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
-- VALUES('age_from', 'Věk od', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'int'));
--
-- --Věk do
-- INSERT INTO ATTRIBUTE
-- (NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
-- VALUES('age_to', 'Věk do', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'int'));
--
-- --Trvání zápasu
-- INSERT INTO ATTRIBUTE
-- (NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
-- VALUES('figth_duration', 'Trvání zápasu', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'int'));
--
-- --Golden score
-- INSERT INTO ATTRIBUTE
-- (NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
-- VALUES('golden_score', 'Golden score', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'string'));
--
-- -- menu
-- INSERT INTO agenda  (name, icon, is_importable) VALUES('competition_category', 'fad fa-boxes', false);
--
-- INSERT INTO menu_link (name, presenter, action, parameters) VALUES('competition_category_default', 'Agenda:Agenda', 'default', '{"name":"competition_category"}');
-- INSERT INTO menu_link (name, presenter, action, parameters) VALUES('competition_category_detail', 'Agenda:Agenda', 'detail', '{"name":"competition_category"}');
--
-- INSERT INTO menu_item (id_menu_section, id_agenda, name, icon, id_menu_link, priority, is_default)
-- VALUES(
--           (select id from menu_section where name = 'basic_evidence')
--       , (select id from agenda where name = 'competition_category')
--       , 'competition_category', 'fad fa-boxes'
--       , (select id from menu_link where name = 'competition_category_default'), 7, false);
--
-- INSERT INTO menu_item_has_menu_link (id_menu_item, id_menu_link)
-- VALUES(
--           (select id from menu_item where name = 'competition_category')
--       , (select id from menu_link where name = 'competition_category_detail')
--       );
--
-- INSERT INTO agenda_segment ("name", source_table, file_source_table) VALUES('competition_category_main', 'agenda_values', 'agenda_has_file');
--
-- --members_main
-- INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
-- VALUES('competition_category_main', false, NULL);
--
-- --agenda_has_segment_definition
-- INSERT INTO agenda_has_segment_definition (id_agenda, id_agenda_segment, priority, is_main)
-- VALUES(
--           (select id from agenda where name = 'competition_category')
--       , (select id from agenda_segment where name = 'competition_category_main')
--       , 1, true
--       );
--
-- --agenda_has_segment_has_attribute_group_definition
-- INSERT INTO agenda_has_segment_has_attribute_group_definition (id_agenda_has_segment_definition, id_agenda_attribute_group, is_read_only, priority)
-- VALUES(
--           (select id from agenda_has_segment_definition
--            where id_agenda = (select id from agenda where name = 'competition_category')
--              and id_agenda_segment = (select id from agenda_segment where name = 'competition_category_main')
--           )
--       , (select id from agenda_attribute_group where name = 'competition_category_main')
--       , false, 1
-- );
--
-- --Ročník
-- INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
--                               , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
-- VALUES(
--           (select id from attribute where name = 'dt_season_year')
--       , 10, true, NULL, false, false, NULL, 3, 0, 1, true, true, 3, false, false
--       , (select id from agenda_has_segment_has_attribute_group_definition
--          where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'competition_category')
--                                                                                                   and id_agenda_segment = (select id from agenda_segment where name = 'competition_category_main')
--          )
--            and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'competition_category_main')
--           )
--       );
-- -- Pohlaví
-- INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
--                               , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
-- VALUES(
--           (select id from attribute where name = 'gender')
--       , 20, true, NULL, false, false, NULL, 3, 0, 1, true, true, 3, false, false
--       , (select id from agenda_has_segment_has_attribute_group_definition
--          where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'competition_category')
--                                                                                                   and id_agenda_segment = (select id from agenda_segment where name = 'competition_category_main')
--          )
--            and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'competition_category_main')
--           )
--       );
-- -- Název
-- INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
--                               , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
-- VALUES(
--           (select id from attribute where name = 'name')
--       , 30, true, NULL, false, false, NULL, 3, 0, 1, true, true, 3, false, false
--       , (select id from agenda_has_segment_has_attribute_group_definition
--          where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'competition_category')
--                                                                                                   and id_agenda_segment = (select id from agenda_segment where name = 'competition_category_main')
--          )
--            and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'competition_category_main')
--           )
--       );
--
-- -- Zkratka
-- INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
--                               , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
-- VALUES(
--           (select id from attribute where name = 'abbreviation')
--       , 40, true, NULL, false, false, NULL, 3, 0, 1, true, true, 3, false, false
--       , (select id from agenda_has_segment_has_attribute_group_definition
--          where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'competition_category')
--                                                                                                   and id_agenda_segment = (select id from agenda_segment where name = 'competition_category_main')
--          )
--            and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'competition_category_main')
--           )
--       );
--
-- -- Věk od
-- INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
--                               , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
-- VALUES(
--           (select id from attribute where name = 'age_from')
--       , 50, true, NULL, false, false, NULL, 3, 0, 1, true, false, 3, false, false
--       , (select id from agenda_has_segment_has_attribute_group_definition
--          where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'competition_category')
--                                                                                                   and id_agenda_segment = (select id from agenda_segment where name = 'competition_category_main')
--          )
--            and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'competition_category_main')
--           )
--       );
--
-- -- Věk do
-- INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
--                               , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
-- VALUES(
--           (select id from attribute where name = 'age_to')
--       , 60, true, NULL, false, false, NULL, 3, 0, 1, true, false, 3, false, false
--       , (select id from agenda_has_segment_has_attribute_group_definition
--          where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'competition_category')
--                                                                                                   and id_agenda_segment = (select id from agenda_segment where name = 'competition_category_main')
--          )
--            and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'competition_category_main')
--           )
--       );
--
-- -- Název váhové kategorie
-- INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
--                               , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
-- VALUES(
--           (select id from attribute where name = 'weight_name')
--       , 70, false, NULL, false, false, NULL, 3, 0, 1, true, false, 3, false, false
--       , (select id from agenda_has_segment_has_attribute_group_definition
--          where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'competition_category')
--                                                                                                   and id_agenda_segment = (select id from agenda_segment where name = 'competition_category_main')
--          )
--            and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'competition_category_main')
--           )
--       );
--
-- -- Váha od
-- INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
--                               , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
-- VALUES(
--           (select id from attribute where name = 'weight_from')
--       , 90, false, NULL, false, false, NULL, 3, 0, 1, false, false, 3, false, false
--       , (select id from agenda_has_segment_has_attribute_group_definition
--          where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'competition_category')
--                                                                                                   and id_agenda_segment = (select id from agenda_segment where name = 'competition_category_main')
--          )
--            and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'competition_category_main')
--           )
--       );
--
-- -- Váha do
-- INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
--                               , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
-- VALUES(
--           (select id from attribute where name = 'weight_to')
--       , 100, false, NULL, false, false, NULL, 3, 0, 1, false, false, 3, false, false
--       , (select id from agenda_has_segment_has_attribute_group_definition
--          where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'competition_category')
--                                                                                                   and id_agenda_segment = (select id from agenda_segment where name = 'competition_category_main')
--          )
--            and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'competition_category_main')
--           )
--       );
--
--
-- -- Trvání zápasu
-- INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
--                               , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
-- VALUES(
--           (select id from attribute where name = 'figth_duration')
--       , 110, true, NULL, false, false, NULL, 3, 0, 1, false, false, 3, false, false
--       , (select id from agenda_has_segment_has_attribute_group_definition
--          where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'competition_category')
--                                                                                                   and id_agenda_segment = (select id from agenda_segment where name = 'competition_category_main')
--          )
--            and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'competition_category_main')
--           )
--       );
--
--
-- -- Golden score
-- INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
--                               , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
-- VALUES(
--           (select id from attribute where name = 'golden_score')
--       , 120, true, NULL, false, false, NULL, 3, 0, 1, false, false, 3, false, false
--       , (select id from agenda_has_segment_has_attribute_group_definition
--          where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'competition_category')
--                                                                                                   and id_agenda_segment = (select id from agenda_segment where name = 'competition_category_main')
--          )
--            and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'competition_category_main')
--           )
--       );

-----------------------------------------------------
-- added 28.06.2023 08:55
-- #13399
INSERT INTO api_agenda_page_attribute (name) VALUES ('kyu_dt_granted');
INSERT INTO api_agenda_page_attribute (name) VALUES ('dan_dt_granted');

refresh materialized view mv_agenda_definition;

INSERT INTO api_agenda_definition_publish (id_external_application, id_agenda_definition) VALUES ((select ID from external_application WHERE name = 'hajime'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'kyu' AND attribute_group_name = 'kyu' AND attribute_name = 'dt_granted'));
INSERT INTO api_agenda_definition_publish (id_external_application, id_agenda_definition) VALUES ((select ID from external_application WHERE name = 'hajime'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'dan' AND attribute_group_name = 'dan' AND attribute_name = 'dt_granted'));
INSERT INTO api_agenda_page_template_has_page_attribute (id_api_agenda_page_template, id_api_agenda_page_attribute) VALUES ((SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member'), (SELECT id FROM api_agenda_page_attribute WHERE name = 'kyu_dt_granted'));
INSERT INTO api_agenda_page_template_has_page_attribute (id_api_agenda_page_template, id_api_agenda_page_attribute) VALUES ((SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member'), (SELECT id FROM api_agenda_page_attribute WHERE name = 'dan_dt_granted'));
INSERT INTO api_agenda_page_attribute_mapping (id_api_agenda_page_template_has_page_attribute, id_api_agenda_definition_publish) VALUES ((SELECT id FROM api_agenda_page_template_has_page_attribute WHERE id_api_agenda_page_template = (SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member') AND id_api_agenda_page_attribute = (SELECT id FROM api_agenda_page_attribute WHERE name = 'kyu_dt_granted')), (SELECT id FROM api_agenda_definition_publish WHERE id_external_application = (select ID from external_application WHERE name = 'hajime') AND id_agenda_definition = (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'kyu' AND attribute_group_name = 'kyu' AND attribute_name = 'dt_granted')));
INSERT INTO api_agenda_page_attribute_mapping (id_api_agenda_page_template_has_page_attribute, id_api_agenda_definition_publish) VALUES ((SELECT id FROM api_agenda_page_template_has_page_attribute WHERE id_api_agenda_page_template = (SELECT id FROM api_agenda_page_template WHERE name = 'hajime_member') AND id_api_agenda_page_attribute = (SELECT id FROM api_agenda_page_attribute WHERE name = 'dan_dt_granted')), (SELECT id FROM api_agenda_definition_publish WHERE id_external_application = (select ID from external_application WHERE name = 'hajime') AND id_agenda_definition = (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'dan' AND attribute_group_name = 'dan' AND attribute_name = 'dt_granted')));

-----------------------------------------------------
-- added 30.06.2023 06:45
-- #13557
TRUNCATE TABLE lookup_categories;
TRUNCATE TABLE lookup_weightcategories;

alter table lookup_categories add valid_from smallint not null;

alter table lookup_categories add match_time integer not null;
comment on column lookup_categories.match_time is 'Match time in seconds';

alter table lookup_categories add is_golden_score bool not null;
alter table lookup_categories add golden_score_time integer default null;
comment on column lookup_categories.golden_score_time is 'Golden score time in seconds';

alter table lookup_categories alter column gender set not null;
alter table lookup_categories alter column age_from set not null;
alter table lookup_categories alter column age_to set not null;
alter table lookup_categories add year_from smallint not null;
alter table lookup_categories add year_to smallint not null;
alter table lookup_categories alter column age_from type smallint using age_from::smallint;
alter table lookup_categories alter column age_to type smallint using age_to::smallint;
alter table lookup_categories drop column inserted;
alter table lookup_categories drop column changed;
alter table lookup_categories drop column deleted;
alter table lookup_categories alter column internal_id drop not null;
alter table lookup_categories alter column dt_upd set default CURRENT_TIMESTAMP;
alter table lookup_categories alter column dt_upd set not null;
alter table lookup_categories add description_short varchar(10) not null;

alter table lookup_categories
    add constraint unq_lookup_categories_gender_age
        unique (valid_from, gender, age_from, age_to);

alter table lookup_categories
    add constraint unq_lookup_categories_name
        unique (valid_from, name);


INSERT INTO lookup_categories (name, gender, age_from, age_to, year_from, year_to, description, description_short, valid_from, match_time, is_golden_score, golden_score_time, priority) VALUES('2023_u8', 'B', 7, 7, 2016, 2016, 'Mladší přípravka U8', 'U8', 2023, 120, true, 60, 100);
INSERT INTO lookup_categories (name, gender, age_from, age_to, year_from, year_to, description, description_short, valid_from, match_time, is_golden_score, golden_score_time, priority) VALUES('2023_u10', 'B', 8, 9, 2014, 2015, 'Mláďata U10', 'U10', 2023, 120, true, 60, 200);
INSERT INTO lookup_categories (name, gender, age_from, age_to, year_from, year_to, description, description_short, valid_from, match_time, is_golden_score, golden_score_time, priority) VALUES('2023_mu12', 'M', 10, 11, 2012, 2013, 'Mladší žáci MU12', 'MU12', 2023, 120, true, 60, 300);
INSERT INTO lookup_categories (name, gender, age_from, age_to, year_from, year_to, description, description_short, valid_from, match_time, is_golden_score, golden_score_time, priority) VALUES('2023_mu14', 'M', 12, 13, 2010, 2011, 'Starší žáci MU14', 'MU14', 2023, 180, true, 60, 400);
INSERT INTO lookup_categories (name, gender, age_from, age_to, year_from, year_to, description, description_short, valid_from, match_time, is_golden_score, golden_score_time, priority) VALUES('2023_mu16', 'M', 14, 15, 2008, 2009, 'Mladší dorostenci MU16', 'MU16', 2023, 180, true, NULL, 500);
INSERT INTO lookup_categories (name, gender, age_from, age_to, year_from, year_to, description, description_short, valid_from, match_time, is_golden_score, golden_score_time, priority) VALUES('2023_mu18', 'M', 16, 17, 2006, 2007, 'Dorostenci MU18', 'MU18', 2023, 240, true, null, 600);
INSERT INTO lookup_categories (name, gender, age_from, age_to, year_from, year_to, description, description_short, valid_from, match_time, is_golden_score, golden_score_time, priority) VALUES('2023_mu21', 'M', 18, 20, 2003, 2005, 'Junioři MU21', 'MU21', 2023, 240, true, null, 700);
INSERT INTO lookup_categories (name, gender, age_from, age_to, year_from, year_to, description, description_short, valid_from, match_time, is_golden_score, golden_score_time, priority) VALUES('2023_muzi', 'M', 21, 100, 2002, 2102, 'Muži', 'Muži', 2023, 240, true, null, 800);
INSERT INTO lookup_categories (name, gender, age_from, age_to, year_from, year_to, description, description_short, valid_from, match_time, is_golden_score, golden_score_time, priority) VALUES('2023_wu12', 'W', 10, 11, 2012, 2013, 'Mladší žáčky WU12', 'WU12', 2023, 120, true, 60, 900);
INSERT INTO lookup_categories (name, gender, age_from, age_to, year_from, year_to, description, description_short, valid_from, match_time, is_golden_score, golden_score_time, priority) VALUES('2023_wu14', 'W', 12, 13, 2010, 2011, 'Starší žáčky WU14', 'WU14', 2023, 180, true, 60, 1000);
INSERT INTO lookup_categories (name, gender, age_from, age_to, year_from, year_to, description, description_short, valid_from, match_time, is_golden_score, golden_score_time, priority) VALUES('2023_wu16', 'W', 14, 15, 2008, 2009, 'Mladší dorostenky WU16', 'WU16', 2023, 180, true, NULL, 1100);
INSERT INTO lookup_categories (name, gender, age_from, age_to, year_from, year_to, description, description_short, valid_from, match_time, is_golden_score, golden_score_time, priority) VALUES('2023_wu18', 'W', 16, 17, 2006, 2007, 'Dorostenky WU18', 'WU18', 2023, 240, true, null, 1200);
INSERT INTO lookup_categories (name, gender, age_from, age_to, year_from, year_to, description, description_short, valid_from, match_time, is_golden_score, golden_score_time, priority) VALUES('2023_wu21', 'W', 18, 20, 2003, 2005, 'Juniorky WU21', 'WU21', 2023, 240, true, null, 1300);
INSERT INTO lookup_categories (name, gender, age_from, age_to, year_from, year_to, description, description_short, valid_from, match_time, is_golden_score, golden_score_time, priority) VALUES('2023_zeny', 'W', 21, 100, 2002, 2102, 'Ženy', 'Ženy', 2023, 240, true, null, 1400);

alter table lookup_weightcategories add id_lookup_categories integer not null;
alter table lookup_weightcategories alter column internal_id drop not null;
alter table lookup_weightcategories add is_hidden bool default FALSE not null;
alter table lookup_weightcategories add id_lookup_weightcategories_plus integer default null;

alter table lookup_weightcategories
    add constraint fk_lookup_weightcategories_id_plus
        foreign key (id_lookup_weightcategories_plus) references lookup_weightcategories;

alter table lookup_weightcategories
    add constraint unq_lookup_weightcategories_name
        unique (id_lookup_categories, name);

alter table lookup_weightcategories
    add constraint unq_lookup_weightcategories_weight
        unique (id_lookup_categories, weight_from, weight_to);

alter table lookup_weightcategories
    add constraint fk_lookup_weightcategories_lookup_categories_id
        foreign key (id_lookup_categories) references lookup_categories
            on delete cascade;

alter table lookup_weightcategories drop column inserted;
alter table lookup_weightcategories drop column changed;
alter table lookup_weightcategories drop column deleted;

alter table lookup_weightcategories alter column weight_from type integer using weight_from::integer;
alter table lookup_weightcategories alter column weight_from set not null;
alter table lookup_weightcategories alter column weight_to type integer using weight_to::integer;
alter table lookup_weightcategories alter column weight_to set not null;
alter table lookup_weightcategories alter column dt_upd set not null;
alter table lookup_weightcategories alter column dt_upd set default CURRENT_TIMESTAMP;

-- (SELECT id FROM lookup_weightcategories WHERE name = '' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_'));

INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_u8'), 'all', 'U8', 0, 999, FALSE, 5);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_u10'), 'all', 'U10', 0, 999, FALSE, 10);

-- MU12
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu12'), '+60 kg', '+60 kg', 61, 999, FALSE, 100);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu12'), '+29 kg', '+29 kg', 30, 999, true, 15);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu12'), '+32 kg', '+32 kg', 33, 999, true, 25);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu12'), '+35 kg', '+35 kg', 36, 999, true, 35);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu12'), '+38 kg', '+38 kg', 39, 999, true, 45);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu12'), '+42 kg', '+42 kg', 43, 999, true, 55);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu12'), '+46 kg', '+46 kg', 47, 999, true, 65);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu12'), '+50 kg', '+50 kg', 51, 999, true, 75);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu12'), '+55 kg', '+55 kg', 56, 999, true, 85);

INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu12'), '-29 kg', '-29 kg', 0, 29, FALSE, 10, (SELECT id FROM lookup_weightcategories WHERE name = '+29 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu12')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu12'), '-32 kg', '-32 kg', 30, 32, FALSE, 20, (SELECT id FROM lookup_weightcategories WHERE name = '+32 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu12')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu12'), '-35 kg', '-35 kg', 33, 35, FALSE, 30, (SELECT id FROM lookup_weightcategories WHERE name = '+35 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu12')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu12'), '-38 kg', '-38 kg', 36, 38, FALSE, 40, (SELECT id FROM lookup_weightcategories WHERE name = '+38 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu12')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu12'), '-42 kg', '-42 kg', 39, 42, FALSE, 50, (SELECT id FROM lookup_weightcategories WHERE name = '+42 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu12')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu12'), '-46 kg', '-46 kg', 43, 46, FALSE, 60, (SELECT id FROM lookup_weightcategories WHERE name = '+46 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu12')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu12'), '-50 kg', '-50 kg', 47, 50, FALSE, 70, (SELECT id FROM lookup_weightcategories WHERE name = '+50 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu12')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu12'), '-55 kg', '-55 kg', 51, 55, FALSE, 80, (SELECT id FROM lookup_weightcategories WHERE name = '+55 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu12')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu12'), '-60 kg', '-60 kg', 56, 60, FALSE, 90, null);

-- mu14
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu14'), '+73 kg', '+73 kg', 74, 999, FALSE, 110);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu14'), '+30 kg', '+30 kg', 31, 999,true, 15);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu14'), '+34 kg', '+34 kg', 35, 999,true, 25);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu14'), '+38 kg', '+38 kg', 39, 999,true, 35);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu14'), '+42 kg', '+42 kg', 43, 999,true, 45);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu14'), '+46 kg', '+46 kg', 47, 999,true, 55);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu14'), '+50 kg', '+50 kg', 51, 999,true, 65);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu14'), '+55 kg', '+55 kg', 56, 999,true, 75);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu14'), '+60 kg', '+60 kg', 61, 999,true, 85);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu14'), '+66 kg', '+66 kg', 67, 999,true, 95);

INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu14'), '-30 kg', '-30 kg', 0, 30, FALSE, 10, (SELECT id FROM lookup_weightcategories WHERE name = '+30 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu14')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu14'), '-34 kg', '-34 kg', 31, 34, FALSE, 20, (SELECT id FROM lookup_weightcategories WHERE name = '+34 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu14')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu14'), '-38 kg', '-38 kg', 35, 38, FALSE, 30, (SELECT id FROM lookup_weightcategories WHERE name = '+38 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu14')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu14'), '-42 kg', '-42 kg', 39, 42, FALSE, 40, (SELECT id FROM lookup_weightcategories WHERE name = '+42 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu14')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu14'), '-46 kg', '-46 kg', 43, 46, FALSE, 50, (SELECT id FROM lookup_weightcategories WHERE name = '+46 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu14')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu14'), '-50 kg', '-50 kg', 47, 50, FALSE, 60, (SELECT id FROM lookup_weightcategories WHERE name = '+50 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu14')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu14'), '-55 kg', '-55 kg', 51, 55, FALSE, 70, (SELECT id FROM lookup_weightcategories WHERE name = '+55 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu14')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu14'), '-60 kg', '-60 kg', 56, 60, FALSE, 80, (SELECT id FROM lookup_weightcategories WHERE name = '+60 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu14')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu14'), '-66 kg', '-66 kg', 61, 66, FALSE, 90, (SELECT id FROM lookup_weightcategories WHERE name = '+66 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu14')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu14'), '-73 kg', '-73 kg', 67, 73, FALSE, 100, null);

-- mu16
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu16'), '+81 kg', '+81 kg', 82, 999, FALSE, 100);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu16'), '+38 kg', '+38 kg', 39, 999, true, 15);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu16'), '+42 kg', '+42 kg', 43, 999, true, 25);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu16'), '+46 kg', '+46 kg', 47, 999, true, 35);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu16'), '+50 kg', '+50 kg', 51, 999, true, 45);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu16'), '+55 kg', '+55 kg', 56, 999, true, 55);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu16'), '+60 kg', '+60 kg', 61, 999, true, 65);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu16'), '+66 kg', '+66 kg', 67, 999, true, 75);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu16'), '+73 kg', '+73 kg', 74, 999, true, 85);

INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu16'), '-38 kg', '-38 kg', 0, 38, FALSE, 10, (SELECT id FROM lookup_weightcategories WHERE name = '+38 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu16')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu16'), '-42 kg', '-42 kg', 39, 42, FALSE, 20, (SELECT id FROM lookup_weightcategories WHERE name = '+42 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu16')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu16'), '-46 kg', '-46 kg', 43, 46, FALSE, 30, (SELECT id FROM lookup_weightcategories WHERE name = '+46 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu16')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu16'), '-50 kg', '-50 kg', 47, 50, FALSE, 40, (SELECT id FROM lookup_weightcategories WHERE name = '+50 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu16')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu16'), '-55 kg', '-55 kg', 51, 55, FALSE, 50, (SELECT id FROM lookup_weightcategories WHERE name = '+55 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu16')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu16'), '-60 kg', '-60 kg', 56, 60, FALSE, 60, (SELECT id FROM lookup_weightcategories WHERE name = '+60 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu16')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu16'), '-66 kg', '-66 kg', 61, 66, FALSE, 70, (SELECT id FROM lookup_weightcategories WHERE name = '+66 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu16')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu16'), '-73 kg', '-73 kg', 67, 73, FALSE, 80, (SELECT id FROM lookup_weightcategories WHERE name = '+73 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu16')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu16'), '-81 kg', '-81 kg', 74, 81, FALSE, 90, null);

--mu18
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu18'), '+90 kg', '+90 kg', 91, 999, FALSE, 80);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu18'), '+50 kg', '+50 kg', 51, 999, true, 15);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu18'), '+55 kg', '+55 kg', 56, 999, true, 25);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu18'), '+60 kg', '+60 kg', 61, 999, true, 35);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu18'), '+66 kg', '+66 kg', 67, 999, true, 45);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu18'), '+73 kg', '+73 kg', 74, 999, true, 55);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu18'), '+81 kg', '+81 kg', 82, 999, true, 65);

INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu18'), '-50 kg', '-50 kg', 0, 50, FALSE, 10, (SELECT id FROM lookup_weightcategories WHERE name = '+50 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu18')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu18'), '-55 kg', '-55 kg', 51, 55, FALSE, 20, (SELECT id FROM lookup_weightcategories WHERE name = '+55 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu18')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu18'), '-60 kg', '-60 kg', 56, 60, FALSE, 30, (SELECT id FROM lookup_weightcategories WHERE name = '+60 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu18')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu18'), '-66 kg', '-66 kg', 61, 66, FALSE, 40, (SELECT id FROM lookup_weightcategories WHERE name = '+66 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu18')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu18'), '-73 kg', '-73 kg', 67, 73, FALSE, 50, (SELECT id FROM lookup_weightcategories WHERE name = '+73 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu18')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu18'), '-81 kg', '-81 kg', 74, 81, FALSE, 60, (SELECT id FROM lookup_weightcategories WHERE name = '+81 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu18')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu18'), '-90 kg', '-90 kg', 82, 90, FALSE, 70, null);

--mu21
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu21'), '+100 kg', '+100 kg', 101, 999, FALSE, 70);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu21'), '+60 kg', '+60 kg', 61, 999, true, 15);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu21'), '+66 kg', '+66 kg', 68, 999, true, 25);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu21'), '+73 kg', '+73 kg', 74, 999, true, 35);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu21'), '+81 kg', '+81 kg', 82, 999, true, 45);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu21'), '+90 kg', '+90 kg', 91, 999, true, 55);

INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu21'), '-60 kg', '-60 kg', 0, 60, FALSE, 10, (SELECT id FROM lookup_weightcategories WHERE name = '+60 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu21')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu21'), '-66 kg', '-66 kg', 61, 66, FALSE, 20, (SELECT id FROM lookup_weightcategories WHERE name = '+66 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu21')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu21'), '-73 kg', '-73 kg', 67, 73, FALSE, 30, (SELECT id FROM lookup_weightcategories WHERE name = '+73 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu21')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu21'), '-81 kg', '-81 kg', 74, 81, FALSE, 40, (SELECT id FROM lookup_weightcategories WHERE name = '+81 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu21')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu21'), '-90 kg', '-90 kg', 82, 90, FALSE, 50, (SELECT id FROM lookup_weightcategories WHERE name = '+90 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu21')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu21'), '-100 kg', '-100 kg', 91, 100, FALSE, 60, null);

--muži
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_muzi'), '+100 kg', '+100 kg', 101, 999, FALSE, 70);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_muzi'), '+60 kg', '+60 kg', 61, 999, true, 15);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_muzi'), '+66 kg', '+66 kg', 68, 999, true, 25);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_muzi'), '+73 kg', '+73 kg', 74, 999, true, 35);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_muzi'), '+81 kg', '+81 kg', 82, 999, true, 45);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_muzi'), '+90 kg', '+90 kg', 91, 999, true, 55);

INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_muzi'), '-60 kg', '-60 kg', 0, 60, FALSE, 10, (SELECT id FROM lookup_weightcategories WHERE name = '+60 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_muzi')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_muzi'), '-66 kg', '-66 kg', 61, 66, FALSE, 20, (SELECT id FROM lookup_weightcategories WHERE name = '+66 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_muzi')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_muzi'), '-73 kg', '-73 kg', 67, 73, FALSE, 30, (SELECT id FROM lookup_weightcategories WHERE name = '+73 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_muzi')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_muzi'), '-81 kg', '-81 kg', 74, 81, FALSE, 40, (SELECT id FROM lookup_weightcategories WHERE name = '+81 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_muzi')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_muzi'), '-90 kg', '-90 kg', 82, 90, FALSE, 50, (SELECT id FROM lookup_weightcategories WHERE name = '+90 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_muzi')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_muzi'), '-100 kg', '-100 kg', 91, 100, FALSE, 60, null);

--wu12
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu12'), '+57 kg', '+57 kg', 58, 999, false, 100);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu12'), '+27 kg', '+27 kg', 28, 999, true, 15);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu12'), '+30 kg', '+30 kg', 31, 999, true, 25);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu12'), '+33 kg', '+33 kg', 34, 999, true, 35);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu12'), '+36 kg', '+36 kg', 37, 999, true, 45);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu12'), '+40 kg', '+40 kg', 41, 999, true, 55);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu12'), '+44 kg', '+44 kg', 45, 999, true, 65);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu12'), '+48 kg', '+48 kg', 49, 999, true, 75);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu12'), '+52 kg', '+52 kg', 53, 999, true, 85);

INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu12'), '-27 kg', '-27 kg', 0, 27, false, 10, (SELECT id FROM lookup_weightcategories WHERE name = '+27 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu12')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu12'), '-30 kg', '-30 kg', 28, 30, false, 20, (SELECT id FROM lookup_weightcategories WHERE name = '+30 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu12')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu12'), '-33 kg', '-33 kg', 31, 33, false, 30, (SELECT id FROM lookup_weightcategories WHERE name = '+33 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu12')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu12'), '-36 kg', '-36 kg', 34, 36, false, 40, (SELECT id FROM lookup_weightcategories WHERE name = '+36 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu12')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu12'), '-40 kg', '-40 kg', 37, 40, false, 50, (SELECT id FROM lookup_weightcategories WHERE name = '+40 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu12')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu12'), '-44 kg', '-44 kg', 41, 44, false, 60, (SELECT id FROM lookup_weightcategories WHERE name = '+44 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu12')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu12'), '-48 kg', '-48 kg', 45, 48, false, 70, (SELECT id FROM lookup_weightcategories WHERE name = '+48 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu12')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu12'), '-52 kg', '-52 kg', 49, 52, false, 80, (SELECT id FROM lookup_weightcategories WHERE name = '+52 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu12')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu12'), '-57 kg', '-57 kg', 53, 57, false, 90, null);

--wu14
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu14'), '+70 kg', '+70 kg', 71, 999, false, 110);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu14'), '+30 kg', '+30 kg', 31, 999, true, 15);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu14'), '+33 kg', '+33 kg', 34, 999, true, 25);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu14'), '+36 kg', '+36 kg', 37, 999, true, 35);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu14'), '+40 kg', '+40 kg', 41, 999, true, 45);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu14'), '+44 kg', '+44 kg', 45, 999, true, 55);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu14'), '+48 kg', '+48 kg', 49, 999, true, 65);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu14'), '+52 kg', '+52 kg', 53, 999, true, 75);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu14'), '+57 kg', '+57 kg', 58, 999, true, 85);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu14'), '+63 kg', '+63 kg', 64, 999, true, 95);

INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu14'), '-30 kg', '-30 kg', 0, 30, false, 10, (SELECT id FROM lookup_weightcategories WHERE name = '+30 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu14')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu14'), '-33 kg', '-33 kg', 31, 33, false, 20, (SELECT id FROM lookup_weightcategories WHERE name = '+33 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu14')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu14'), '-36 kg', '-36 kg', 34, 36, false, 30, (SELECT id FROM lookup_weightcategories WHERE name = '+36 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu14')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu14'), '-40 kg', '-40 kg', 37, 40, false, 40, (SELECT id FROM lookup_weightcategories WHERE name = '+40 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu14')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu14'), '-44 kg', '-44 kg', 41, 44, false, 50, (SELECT id FROM lookup_weightcategories WHERE name = '+44 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu14')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu14'), '-48 kg', '-48 kg', 45, 48, false, 60, (SELECT id FROM lookup_weightcategories WHERE name = '+48 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu14')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu14'), '-52 kg', '-52 kg', 49, 52, false, 70, (SELECT id FROM lookup_weightcategories WHERE name = '+52 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu14')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu14'), '-57 kg', '-57 kg', 53, 57, false, 80, (SELECT id FROM lookup_weightcategories WHERE name = '+57 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu14')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu14'), '-63 kg', '-63 kg', 58, 63, false, 90, (SELECT id FROM lookup_weightcategories WHERE name = '+63 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu14')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu14'), '-70 kg', '-70 kg', 64, 70, false, 100, null);


--wu16
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu16'), '+78 kg', '+78 kg', 79, 999, false, 100);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu16'), '+36 kg', '+36 kg', 37, 999, true, 15);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu16'), '+40 kg', '+40 kg', 41, 999, true, 25);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu16'), '+44 kg', '+44 kg', 45, 999, true, 35);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu16'), '+48 kg', '+48 kg', 49, 999, true, 45);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu16'), '+52 kg', '+52 kg', 53, 999, true, 55);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu16'), '+57 kg', '+57 kg', 58, 999, true, 65);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu16'), '+63 kg', '+63 kg', 64, 999, true, 75);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu16'), '+70 kg', '+70 kg', 71, 999, true, 85);

INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu16'), '-36 kg', '-36 kg', 0, 36, false, 10, (SELECT id FROM lookup_weightcategories WHERE name = '+36 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu16')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu16'), '-40 kg', '-40 kg', 37, 40, false, 20, (SELECT id FROM lookup_weightcategories WHERE name = '+40 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu16')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu16'), '-44 kg', '-44 kg', 41, 44, false, 30, (SELECT id FROM lookup_weightcategories WHERE name = '+44 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu16')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu16'), '-48 kg', '-48 kg', 45, 48, false, 40, (SELECT id FROM lookup_weightcategories WHERE name = '+48 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu16')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu16'), '-52 kg', '-52 kg', 49, 52, false, 50, (SELECT id FROM lookup_weightcategories WHERE name = '+52 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu16')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu16'), '-57 kg', '-57 kg', 53, 57, false, 60, (SELECT id FROM lookup_weightcategories WHERE name = '+57 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu16')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu16'), '-63 kg', '-63 kg', 58, 63, false, 70, (SELECT id FROM lookup_weightcategories WHERE name = '+63 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu16')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu16'), '-70 kg', '-70 kg', 64, 70, false, 80, (SELECT id FROM lookup_weightcategories WHERE name = '+70 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu16')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu16'), '-78 kg', '-78 kg', 71, 78, false, 90, null);

--wu18
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu18'), '+78 kg', '+78 kg', 79, 999, false, 70);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu18'), '+48 kg', '+48 kg', 49, 999, true, 15);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu18'), '+52 kg', '+52 kg', 53, 999, true, 25);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu18'), '+57 kg', '+57 kg', 58, 999, true, 35);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu18'), '+63 kg', '+63 kg', 64, 999, true, 45);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu18'), '+70 kg', '+70 kg', 71, 999, true, 55);

INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu18'), '-48 kg', '-48 kg', 45, 48, false, 10, (SELECT id FROM lookup_weightcategories WHERE name = '+48 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu18')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu18'), '-52 kg', '-52 kg', 49, 52, false, 20, (SELECT id FROM lookup_weightcategories WHERE name = '+52 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu18')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu18'), '-57 kg', '-57 kg', 53, 57, false, 30, (SELECT id FROM lookup_weightcategories WHERE name = '+57 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu18')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu18'), '-63 kg', '-63 kg', 58, 63, false, 40, (SELECT id FROM lookup_weightcategories WHERE name = '+63 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu18')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu18'), '-70 kg', '-70 kg', 64, 70, false, 50, (SELECT id FROM lookup_weightcategories WHERE name = '+70 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu18')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu18'), '-78 kg', '-78 kg', 71, 78, false, 60, null);


--wu21
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu21'), '+78 kg', '+78 kg', 79, 999, false, 70);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu21'), '+48 kg', '+48 kg', 49, 999, true, 15);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu21'), '+52 kg', '+52 kg', 53, 999, true, 25);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu21'), '+57 kg', '+57 kg', 58, 999, true, 35);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu21'), '+63 kg', '+63 kg', 64, 999, true, 45);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu21'), '+70 kg', '+70 kg', 71, 999, true, 55);

INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu21'), '-48 kg', '-48 kg', 0, 48, false, 10, (SELECT id FROM lookup_weightcategories WHERE name = '+48 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu21')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu21'), '-52 kg', '-52 kg', 49, 52, false, 20, (SELECT id FROM lookup_weightcategories WHERE name = '+52 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu21')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu21'), '-57 kg', '-57 kg', 53, 57, false, 30, (SELECT id FROM lookup_weightcategories WHERE name = '+57 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu21')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu21'), '-63 kg', '-63 kg', 58, 63, false, 40, (SELECT id FROM lookup_weightcategories WHERE name = '+63 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu21')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu21'), '-70 kg', '-70 kg', 64, 70, false, 50, (SELECT id FROM lookup_weightcategories WHERE name = '+70 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu21')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu21'), '-78 kg', '-78 kg', 71, 78, false, 60, null);


--zeny
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_zeny'), '+78 kg', '+78 kg', 79, 999, false, 70);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_zeny'), '+48 kg', '+48 kg', 49, 999, true, 15);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_zeny'), '+52 kg', '+52 kg', 53, 999, true, 25);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_zeny'), '+57 kg', '+57 kg', 58, 999, true, 35);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_zeny'), '+63 kg', '+63 kg', 64, 999, true, 45);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_zeny'), '+70 kg', '+70 kg', 71, 999, true, 55);

INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_zeny'), '-48 kg', '-48 kg', 0, 48, false, 10, (SELECT id FROM lookup_weightcategories WHERE name = '+48 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_zeny')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_zeny'), '-52 kg', '-52 kg', 49, 52, false, 20, (SELECT id FROM lookup_weightcategories WHERE name = '+52 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_zeny')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_zeny'), '-57 kg', '-57 kg', 53, 57, false, 30, (SELECT id FROM lookup_weightcategories WHERE name = '+57 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_zeny')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_zeny'), '-63 kg', '-63 kg', 58, 63, false, 40, (SELECT id FROM lookup_weightcategories WHERE name = '+63 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_zeny')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_zeny'), '-70 kg', '-70 kg', 64, 70, false, 50, (SELECT id FROM lookup_weightcategories WHERE name = '+70 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_zeny')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_zeny'), '-78 kg', '-78 kg', 71, 78, false, 60, null);


CREATE MATERIALIZED VIEW flexii.lookup_judo_categories AS
SELECT lw.id,
       lc.id AS id_lookup_categories,
       lw.id_lookup_weightcategories_plus,
       lc.name || '_' || coalesce(lw.name, '') AS name,
       lc.description || ' ' || coalesce(lw.description, '') AS description,
       lc.name                                 AS category_name,
       lw.name                                 AS weight_name,
       lc.description AS category_description,
       lc.description_short AS category_description_short,
       COALESCE(lw.description, lc.description_short) AS weight_description,
       lc.gender,
       lc.age_from,
       lc.age_to,
       lc.year_from,
       lc.year_to,
       lw.weight_from,
       lw.weight_to,
       lc.match_time,
       lc.is_golden_score,
       lc.golden_score_time,
       coalesce(lw.is_hidden, false) AS is_hidden,
       lc.priority + COALESCE(lw.priority,0) AS priority,
       CASE WHEN lw.enabled = true AND lc.enabled = true THEN true ELSE false END AS enabled
FROM lookup_categories lc
         LEFT JOIN lookup_weightcategories lw ON lc.id = lw.id_lookup_categories AND lw.enabled = TRUE
WHERE lc.enabled = TRUE;


DELETE FROM agenda_definition_specification_by_value WHERE id_agenda_definition IN (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournaments' AND segment_name = 'tournament_category' AND attribute_group_name = 'tournament_category_main' AND attribute_name IN ('sex', 'weightcategories'));
DELETE FROM agenda_definition_specification_by_value WHERE id_agenda_definition_control IN (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournaments' AND segment_name = 'tournament_category' AND attribute_group_name = 'tournament_category_main' AND attribute_name IN ('sex', 'weightcategories'));
DELETE FROM agenda_definition WHERE id IN (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournaments' AND segment_name = 'tournament_category' AND attribute_group_name = 'tournament_category_main' AND attribute_name IN ('sex', 'weightcategories'));
UPDATE attribute SET lookup = 'lookup_judo_categories' WHERE name = 'competition_category';


DELETE FROM agenda_has_agenda_definition WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'tournaments_has_members') AND id_attribute IN (select id from attribute where name IN ('weightcategories', 'categories'));

INSERT INTO agenda_has_agenda_definition (id_agenda_has_agenda, id_attribute, priority, is_not_null, is_unique, default_value) VALUES((select id from agenda_has_agenda where name = 'tournaments_has_members')
                                                                                                                                     , (select id from attribute where name = 'competition_category'), 10, false, false, null);

INSERT INTO agenda_button (id_agenda, name, description, icon, class) VALUES ((SELECT id FROM agenda WHERE name = 'tournaments'), 'TournamentPrintPasswords', 'Vytiskni hesla', 'fal fa-print', 'btn-info');

INSERT INTO agenda_action (name, description, class_name, is_via_rabbit)
VALUES ('TournamentPrintPasswords', 'Vytiskni hesla', 'TournamentPrintPasswords', FALSE);

INSERT INTO agenda_button_has_action (id_agenda_button, id_agenda_action, priority) VALUES ((SELECT id FROM agenda_button WHERE name = 'TournamentPrintPasswords'), (SELECT id FROM agenda_action WHERE name = 'TournamentPrintPasswords'), 40);


-- added 10.07.2024 14:30

DELETE FROM agenda_entity_permission_by_definition WHERE id_agenda_definition IN (SELECT id
                                                                                  FROM mv_agenda_definition
                                                                                  WHERE agenda_name = 'tournaments'
                                                                                    AND segment_name = 'tournament_manager');

DELETE
FROM agenda_definition
WHERE id IN (SELECT id
             FROM mv_agenda_definition
             WHERE agenda_name = 'tournaments'
               AND segment_name = 'tournament_manager');

UPDATE agenda_attribute_group SET is_group_recurring = false WHERE name = 'tournament_manager_main';

INSERT INTO agenda_has_segment_has_attribute_group_definition (id_agenda_has_segment_definition, id_agenda_attribute_group, is_read_only, priority)
VALUES (
           (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'tournaments')
                                                           and id_agenda_segment = (select id from agenda_segment where name = 'tournament_access')),
           (select id from agenda_attribute_group where name = 'tournament_manager_main'),
           true,
           5
       );

DELETE FROM agenda_has_segment_has_attribute_group_definition WHERE id_agenda_has_segment_definition IN (SELECT id from agenda_segment WHERE name = 'tournament_manager');
DELETE FROM agenda_has_segment_definition WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'tournaments') AND id_agenda_segment = (SELECT id from agenda_segment WHERE name = 'tournament_manager');
DELETE FROM agenda_segment WHERE name = 'tournament_manager';

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'access_password')
      , 10, false, NULL, false, false, 3, 0, 1, false, false, NULL, FALSE, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'tournaments')
                                                                                                  and id_agenda_segment = (select id from agenda_segment where name = 'tournament_access')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_manager_main')
          )
      );


-- added 11.07.2023 7:45
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, WEIGHT, id_attribute_data_type)
VALUES('priority', 'Priorita', 1, 1, (select id from attribute_data_type where name = 'int'));

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'priority')
      , 40, true, NULL, false, true, NULL, 3, 0, 1, FALSE, FALSE, null
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'tournaments')
                                                                                                  and id_agenda_segment = (select id from agenda_segment where name = 'tournament_access')
         )
           and id_agenda_attribute_group= (select id from agenda_attribute_group where name = 'access_passwords')
          )
      );

---------- #13623 ----------
UPDATE agenda_definition SET is_unique = false
WHERE id = ( SELECT id
             FROM mv_agenda_definition
             WHERE attribute_name = 'email'
               AND attribute_group_name = 'tournament_information_main'
               AND segment_name = 'tournament_information'
               AND agenda_name = 'tournaments'
);

UPDATE agenda_definition SET is_unique = false
WHERE id = ( SELECT id
             FROM mv_agenda_definition
             WHERE attribute_name = 'description'
               AND attribute_group_name = 'tournament_information_main'
               AND segment_name = 'tournament_information'
               AND agenda_name = 'tournaments'
);

UPDATE agenda_definition SET is_unique = false
WHERE id = ( SELECT id
             FROM mv_agenda_definition
             WHERE attribute_name = 'dt_of_change'
               AND attribute_group_name = 'tournament_information_main'
               AND segment_name = 'tournament_information'
               AND agenda_name = 'tournaments'
);

----------- #13419 -----------

UPDATE agenda_definition SET pattern = null, is_not_null = true, min_value = 1, max_value = 10
WHERE id = ( SELECT id
             FROM mv_agenda_definition
             WHERE attribute_name = 'tatami_count'
               AND attribute_group_name = 'tournaments_main'
               AND agenda_name = 'tournaments'
               AND segment_name = 'tournaments_main'
);

UPDATE agenda_definition SET pattern = null, is_not_null = true, min_value = 1, max_value = 10
WHERE id = ( SELECT id
             FROM mv_agenda_definition
             WHERE attribute_name = 'scale_count'
               AND attribute_group_name = 'tournaments_main'
               AND agenda_name = 'tournaments'
               AND segment_name = 'tournaments_main'
);

----------- #13419 -----------
alter table lookup_categories add weight_tolerance decimal default 0.1;
UPDATE lookup_categories SET weight_tolerance = 0 WHERE name IN ('2023_mu21','2023_muzi','2023_wu21','2023_zeny');

INSERT INTO lookup_categories (name, gender, age_from, age_to, year_from, year_to, description, description_short, valid_from, match_time, is_golden_score, golden_score_time, weight_tolerance, priority) VALUES('2023_mu23', 'M', 18, 22, 2001, 2005, 'Junioři MU23', 'MU23', 2023, 240, true, null, 0, 750);
INSERT INTO lookup_categories (name, gender, age_from, age_to, year_from, year_to, description, description_short, valid_from, match_time, is_golden_score, golden_score_time, weight_tolerance, priority) VALUES('2023_wu23', 'W', 18, 22, 2001, 2005, 'Juniorky WU23', 'WU23', 2023, 240, true, null, 0, 1350);

--mu23
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu23'), '+100 kg', '+100 kg', 101, 999, FALSE, 70);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu23'), '+60 kg', '+60 kg', 61, 999, true, 15);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu23'), '+66 kg', '+66 kg', 68, 999, true, 25);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu23'), '+73 kg', '+73 kg', 74, 999, true, 35);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu23'), '+81 kg', '+81 kg', 82, 999, true, 45);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu23'), '+90 kg', '+90 kg', 91, 999, true, 55);

INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu23'), '-60 kg', '-60 kg', 0, 60, FALSE, 10, (SELECT id FROM lookup_weightcategories WHERE name = '+60 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu23')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu23'), '-66 kg', '-66 kg', 61, 66, FALSE, 20, (SELECT id FROM lookup_weightcategories WHERE name = '+66 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu23')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu23'), '-73 kg', '-73 kg', 67, 73, FALSE, 30, (SELECT id FROM lookup_weightcategories WHERE name = '+73 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu23')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu23'), '-81 kg', '-81 kg', 74, 81, FALSE, 40, (SELECT id FROM lookup_weightcategories WHERE name = '+81 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu23')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu23'), '-90 kg', '-90 kg', 82, 90, FALSE, 50, (SELECT id FROM lookup_weightcategories WHERE name = '+90 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu23')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_mu23'), '-100 kg', '-100 kg', 91, 100, FALSE, 60, null);

--wu23
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu23'), '+78 kg', '+78 kg', 79, 999, false, 70);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu23'), '+48 kg', '+48 kg', 49, 999, true, 15);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu23'), '+52 kg', '+52 kg', 53, 999, true, 25);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu23'), '+57 kg', '+57 kg', 58, 999, true, 35);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu23'), '+63 kg', '+63 kg', 64, 999, true, 45);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu23'), '+70 kg', '+70 kg', 71, 999, true, 55);

INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu23'), '-48 kg', '-48 kg', 0, 48, false, 10, (SELECT id FROM lookup_weightcategories WHERE name = '+48 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu23')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu23'), '-52 kg', '-52 kg', 49, 52, false, 20, (SELECT id FROM lookup_weightcategories WHERE name = '+52 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu23')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu23'), '-57 kg', '-57 kg', 53, 57, false, 30, (SELECT id FROM lookup_weightcategories WHERE name = '+57 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu23')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu23'), '-63 kg', '-63 kg', 58, 63, false, 40, (SELECT id FROM lookup_weightcategories WHERE name = '+63 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu23')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu23'), '-70 kg', '-70 kg', 64, 70, false, 50, (SELECT id FROM lookup_weightcategories WHERE name = '+70 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu23')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu23'), '-78 kg', '-78 kg', 71, 78, false, 60, null);


----------- #13619 -----------
--Věková kategorie
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type, custom_data_class)
VALUES('age_category', 'Věková kategorie', 0, 'lookup_categories', NULL, 1, (select id from attribute_data_type where name = 'lookup_solo_table'), 'AgeCategoryData');


--Hmotností kategorie
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type, custom_data_class)
VALUES('weight_category', 'Hmotností kategorie', 0, 'lookup_weightcategories', NULL, 1, (select id from attribute_data_type where name = 'lookup_solo_table'), 'WeightCategoryData');


DELETE
FROM agenda_has_agenda_values
WHERE id_agenda_has_agenda_definition = (SELECT id
                                         FROM agenda_has_agenda_definition
                                         WHERE id_agenda_has_agenda = (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_members')
                                           AND id_attribute IN (SELECT id FROM attribute WHERE name IN ('competition_category')));
DELETE FROM agenda_has_agenda_definition WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'tournaments_has_members') AND id_attribute IN (select id from attribute where name IN ('competition_category'));

INSERT INTO agenda_has_agenda_definition (id_agenda_has_agenda, id_attribute, priority, is_not_null, is_unique, default_value)
VALUES ((SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_members')
       , (SELECT id FROM attribute WHERE name = 'weight_category'), 20, FALSE, FALSE, NULL);


INSERT INTO agenda_has_agenda_definition (id_agenda_has_agenda, id_attribute, priority, is_not_null, is_unique, default_value, id_agenda_has_agenda_dependent)
VALUES ((SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_members')
       , (SELECT id FROM attribute WHERE name = 'age_category'), 10, FALSE, FALSE, NULL
       , (SELECT id FROM agenda_has_agenda_definition WHERE id_agenda_has_agenda = (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_members')
                                                        AND id_attribute = (SELECT id FROM attribute WHERE name = 'weight_category'))
       );

--Typ turnaje
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('tournament_type', 'Typ turnaje', 0, 'lookup_tournament_type', NULL, 1, (select id from attribute_data_type where name = 'lookup_solo_table'));


CREATE TABLE flexii.lookup_tournament_type
(
    id          serial       NOT NULL,
    name        varchar(100) NOT NULL,
    description varchar(100),
    is_weight_required boolean NOT NULL,
    priority    integer      NOT NULL DEFAULT 0,
    enabled     boolean      NOT NULL DEFAULT TRUE,
    dt_ins      timestamp    NOT NULL DEFAULT CURRENT_TIMESTAMP,
    dt_upd      timestamp    NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT pk_lookup_tournament_type PRIMARY KEY (id)
);

ALTER TABLE lookup_tournament_type
    OWNER TO flexii;

CREATE TRIGGER tr_lookup_tournament_type_dt_upd
    BEFORE UPDATE
    ON lookup_tournament_type
    FOR EACH ROW
EXECUTE PROCEDURE base.update_dt_upd();

INSERT INTO lookup_tournament_type (name, description, is_weight_required, priority) VALUES ('standard', 'Standardní bez omezení', false, 10);
INSERT INTO lookup_tournament_type (name, description, is_weight_required, priority) VALUES ('standard_weight_required', 'Standardní s povinnou hmotnostní kategorií', true, 20);

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'tournament_type')
      , 22, true, NULL, false, false, NULL, 3, 0, 1, false, false, NULL, false, true
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'tournaments')
                                                                                                  and id_agenda_segment = (select id from agenda_segment where name = 'tournaments_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'tournaments_main')
          )
      );

-- deleting useless attribute
DELETE
FROM agenda_has_agenda_definition
WHERE id_agenda_has_agenda = (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_members')
  AND id_attribute = (SELECT id FROM attribute WHERE name = 'tournaments_has_members');

-----------------------------------
-- Run only AFTER full migration --
----------- #13728 ----------------
-- DROP SEQUENCE IF EXISTS flexii.seq_member_id CASCADE;
CREATE SEQUENCE flexii.seq_member_id
    INCREMENT BY 1
    MINVALUE 0
    MAXVALUE 2147483647
    START WITH 1
    CACHE 1
    NO CYCLE
    OWNED BY NONE;
-- ddl-end --
ALTER SEQUENCE flexii.seq_member_id OWNER TO flexii;

UPDATE attribute SET custom_data_class = 'MemberId' WHERE name = 'id_member';

UPDATE agenda_definition SET is_disabled = TRUE
WHERE id = (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'members_main' AND attribute_group_name = 'members_main' AND attribute_name = 'id_member');

----------- #13727 -----------

INSERT INTO agenda_definition_custom (id_agenda_definition, class_name) VALUES (
                                                                                   (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'clubs' AND segment_name = 'clubs_main' AND attribute_group_name = 'clubs_main' AND attribute_name = 'identifier_idnumber')
                                                                               , 'ClubsAres'
                                                                               );

---
UPDATE agenda_has_agenda SET is_unique = false WHERE name = 'tournaments_has_members';

-- added loockup location
INSERT INTO lookup_locations (internal_id, name, description) VALUES(0,'Neuvedeno', '');

--- #13631 ---
--------------
-- check agenda value with gender child
--------------
-- select * from flexii.agenda_values where id_agenda_definition = (
-- 	SELECT id FROM mv_agenda_definition
-- 	WHERE agenda_name = 'members'
-- 	AND segment_name = 'members_main'
-- 	AND attribute_group_name = 'members_main'
-- 	AND attribute_name = 'gender'
-- ) and value = (select id::VARCHAR from flexii.master_lookup where lookup_name = 'lookup_gender' and name = 'child');

delete from master_lookup where lookup_name = 'lookup_gender' and name = 'child';

update agenda_definition set is_read_only = true
where id = (
    SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'members'
        AND segment_name = 'members_main'
        AND attribute_group_name = 'members_main'
        AND attribute_name = 'dt_birthyear'
);

update agenda_definition set is_not_null = true
where id = (
    SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'members'
        AND segment_name = 'members_main'
        AND attribute_group_name = 'members_main'
        AND attribute_name = 'dt_birthdate'
);

update agenda_definition set is_not_null = true
where id = (
    SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'members'
        AND segment_name = 'members_main'
        AND attribute_group_name = 'members_main'
        AND attribute_name = 'telephone'
);


update agenda_definition set is_not_null = true
where id = (
    SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'members'
        AND segment_name = 'members_main'
        AND attribute_group_name = 'members_main'
        AND attribute_name = 'email'
);

-- Aktivní závodník --
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('active_fighter', 'Aktivní závodník', 0, 'lookup_active_fighter', NULL, 1, (select id from attribute_data_type where name = 'lookup'));

INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES('lookup_active_fighter', 'stat0', '0');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES('lookup_active_fighter', 'stat1', '1');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES('lookup_active_fighter', 'stat2', '2');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES('lookup_active_fighter', 'stat3', '3');

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
, is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'active_fighter')
      , 72, true, (select id from master_lookup where lookup_name = 'lookup_active_fighter' and name='stat0'), false, false, NULL, 3, 0, 1, false, false, NULL, false, true
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
             select id from agenda_has_segment_definition
               where id_agenda = (select id from agenda where name = 'members')
               and id_agenda_segment = (select id from agenda_segment where name = 'members_main')
         )
            and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'members_main')
         )
      );

-- Nezletilost --
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('nonage', 'Nezletilost', 0, 'lookup_yes_no', NULL, 1, (select id from attribute_data_type where name = 'lookup'));

INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES('lookup_yes_no', 'yes', 'Ano');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES('lookup_yes_no', 'no', 'Ne');

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
, is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'nonage')
      , 73, true, false, false, false, NULL, 3, 0, 1, false, false, NULL, false, true
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
             select id from agenda_has_segment_definition
               where id_agenda = (select id from agenda where name = 'members')
               and id_agenda_segment = (select id from agenda_segment where name = 'members_main')
         )
            and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'members_main')
         )
      );

-- Typ identifikátoru --
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('identifier_type', 'Typ identifikátoru', 0, 'lookup_identifier_type', NULL, 1, (select id from attribute_data_type where name = 'lookup'));

INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_identifier_type', 'rc', 'Rodné číslo', 10);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_identifier_type', 'pass', 'Pas', 20);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_identifier_type', 'alt_iden', 'Alternativní identifikátor', 30);

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
, is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'identifier_type')
      , 53, true, false, false, false, NULL, 3, 0, 1, false, false, NULL, false, true
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
             select id from agenda_has_segment_definition
               where id_agenda = (select id from agenda where name = 'members')
               and id_agenda_segment = (select id from agenda_segment where name = 'members_main')
         )
            and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'members_main')
         )
      );

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('identifier_value', 'Rodné číslo/Identifikátor', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'string'));

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
, is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'identifier_value')
      , 54, true, false, false, false, NULL, 3, 0, 1, false, false, NULL, false, true
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
             select id from agenda_has_segment_definition
               where id_agenda = (select id from agenda where name = 'members')
               and id_agenda_segment = (select id from agenda_segment where name = 'members_main')
         )
            and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'members_main')
         )
      );

-------------------------
-- remove phone_office --
-------------------------
-- check agenda_values values --
-------------------------
-- select av.id, ae.uid , av.value from agenda_values av
-- join agenda_entity ae on ae.id = av.id_agenda_entity
-- where id_agenda_definition = (
--     SELECT id FROM mv_agenda_definition
--         WHERE agenda_name = 'members'
--         AND segment_name = 'members_main'
--         AND attribute_group_name = 'members_contact_main'
--         AND attribute_name = 'phone_office'
-- );

--- set diabled to agenda_definition ---
-- update agenda_definition set enabled = false
-- where id = (
--  SELECT id FROM mv_agenda_definition
--     WHERE agenda_name = 'members'
--     AND segment_name = 'members_main'
--     AND attribute_group_name = 'members_contact_main'
--     AND attribute_name = 'phone_office'
-- );

update agenda_definition set is_not_null = true
where id = (
     SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'members'
        AND segment_name = 'members_main'
        AND attribute_group_name = 'avatar_group'
        AND attribute_name = 'avatar'
);

refresh materialized view mv_agenda_definition;

-- If nonage = yes then member parent is not null = true --
INSERT INTO agenda_definition_specification_by_value
    (id_agenda_definition, id_agenda_definition_control, value_control, is_hidden, is_not_null, default_value, is_disabled, is_read_only)
VALUES(
    (SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'members'
        AND segment_name = 'members_main'
        AND attribute_group_name = 'members_main'
        AND attribute_name = 'members_has_parents'
    ), (SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'members'
        AND segment_name = 'members_main'
        AND attribute_group_name = 'members_main'
        AND attribute_name = 'nonage'
    ), (SELECT id FROM master_lookup WHERE enabled = true AND lookup_name = 'lookup_yes_no' AND name = 'yes')
          , false, true, null, false, false
);

-- Report --
-- members dt_birthyear >= 18 and members parent = yes --
-- select
-- 	uid
-- 	, members_main__members_main__id_member
-- 	, members_main__members_main__firstname
-- 	, members_main__members_main__surname
-- 	, members_main__members_main__dt_birthyear
-- 	, (SELECT description FROM master_lookup WHERE id = members_main__members_main__nonage::int) as nonage
-- from dv_agenda_members_data
-- where (SELECT date_part('year', now())) - members_main__members_main__dt_birthyear::int >= 18
-- AND (SELECT description FROM master_lookup WHERE id = members_main__members_main__nonage::int) = 'Ano';


-- Report --
-- members dt_birthyear < 18 and members parent = no --
-- select
-- 	uid
-- 	, members_main__members_main__id_member
-- 	, members_main__members_main__firstname
-- 	, members_main__members_main__surname
-- 	, members_main__members_main__dt_birthyear
-- 	, (SELECT description FROM master_lookup WHERE id = members_main__members_main__nonage::int) as nonage
-- from dv_agenda_members_data
-- where (SELECT date_part('year', now())) - members_main__members_main__dt_birthyear::int < 18
-- AND (SELECT description FROM master_lookup WHERE id = members_main__members_main__nonage::int) = 'Ne';


-- check if exist value 'Statutár' in aha_values
-- select * from agenda_has_agenda_values where id_agenda_has_agenda_definition = (
--     select id from agenda_has_agenda_definition
--         where id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_officials_has_members')
--     AND id_attribute = (select id from attribute where name = 'club_officials_role')
-- ) and value::int = (SELECT id FROM master_lookup WHERE lookup_name = 'lookup_club_officials_role' AND name = 'cor1');

-- Then you should delete it
delete from master_lookup where id = (
    SELECT id FROM master_lookup WHERE lookup_name = 'lookup_club_officials_role' AND name = 'cor1'
);

-- remove column id_member from clubs_officials_has_members
delete from agenda_has_agenda_show_attribute
    where id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_officials_has_members')
and id_agenda_definition = (
    SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'members'
        AND segment_name = 'members_main'
        AND attribute_group_name = 'members_main'
        AND attribute_name = 'id_member'
);

UPDATE agenda_definition SET is_not_null = true
WHERE id = (
     SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'clubs'
        AND segment_name = 'clubs_main'
        AND attribute_group_name = 'clubs_main'
        AND attribute_name = 'abbreviation'
);

UPDATE agenda_definition SET is_not_null = true
WHERE id = (
     SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'clubs'
        AND segment_name = 'clubs_main'
        AND attribute_group_name = 'clubs_main'
        AND attribute_name = 'abbreviation'
);

UPDATE agenda_definition SET is_not_null = true
WHERE id = (
     SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'clubs'
        AND segment_name = 'clubs_main'
        AND attribute_group_name = 'clubs_main'
        AND attribute_name = 'email'
);

UPDATE agenda_definition SET is_not_null = true
WHERE id = (
     SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'clubs'
        AND segment_name = 'clubs_main'
        AND attribute_group_name = 'clubs_main'
        AND attribute_name = 'telephone'
);

-- check agenda_values --
-- select * from agenda_values
--     where id_agenda_definition = (
--      SELECT id FROM mv_agenda_definition
--         WHERE agenda_name = 'clubs'
--         AND segment_name = 'clubs_main'
--         AND attribute_group_name = 'clubs_main'
--         AND attribute_name = 'iscus_id'
-- );
-- remove all value from agenda_values with definition --
delete from agenda_values
    where id_agenda_definition = (
     SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'clubs'
        AND segment_name = 'clubs_main'
        AND attribute_group_name = 'clubs_main'
        AND attribute_name = 'iscus_id'
);

-- remove agenda_definition --
delete from agenda_definition
WHERE id = (
     SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'clubs'
        AND segment_name = 'clubs_main'
        AND attribute_group_name = 'clubs_main'
        AND attribute_name = 'iscus_id'
);

-- check agenda_values --
-- select * from agenda_values
--     where id_agenda_definition = (
--      SELECT id FROM mv_agenda_definition
--         WHERE agenda_name = 'clubs'
--         AND segment_name = 'clubs_main'
--         AND attribute_group_name = 'clubs_main'
--         AND attribute_name = 'is_active'
-- );
-- remove all value from agenda_values with definition --
delete from agenda_values
    where id_agenda_definition = (
     SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'clubs'
        AND segment_name = 'clubs_main'
        AND attribute_group_name = 'clubs_main'
        AND attribute_name = 'is_active'
);

-- remove agenda_definition --
delete from agenda_definition
WHERE id = (
     SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'clubs'
        AND segment_name = 'clubs_main'
        AND attribute_group_name = 'clubs_main'
        AND attribute_name = 'is_active'
);

-- Číslo účtu
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('account_number', 'Číslo účtu', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'string'));

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
, is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'account_number')
      , 55, false, false, false, false, NULL, 3, 0, 1, false, false, NULL, false, true
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
             select id from agenda_has_segment_definition
               where id_agenda = (select id from agenda where name = 'clubs')
               and id_agenda_segment = (select id from agenda_segment where name = 'clubs_main')
         )
            and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'clubs_main')
         )
      );

-- Zápis v rejstříku spolků
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('entry_register_of_associations', 'Zápis v rejstříku spolků', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'url'));

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
, is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'entry_register_of_associations')
      , 65, false, false, true, true, NULL, 3, 0, 1, false, false, NULL, false, true
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
             select id from agenda_has_segment_definition
               where id_agenda = (select id from agenda where name = 'clubs')
               and id_agenda_segment = (select id from agenda_segment where name = 'clubs_main')
         )
            and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'clubs_main')
         )
      );

----- avatar ------
--agenda_has_segment_has_attribute_group_definition
INSERT INTO agenda_has_segment_has_attribute_group_definition (id_agenda_has_segment_definition, id_agenda_attribute_group, is_read_only, priority)
VALUES(
          (select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'clubs')
             and id_agenda_segment = (select id from agenda_segment where name = 'clubs_main')
          )
      , (select id from agenda_attribute_group where name = 'avatar_group')
      , false, 1
      );

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value
                              , is_disabled, is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, id_agenda_segment_attribute_group_definition)
VALUES(
       (select id from attribute where name = 'avatar')
      , 5, true, NULL, false, false, NULL, 3, 0, 1, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
            select id from agenda_has_segment_definition
             where id_agenda = (select id from agenda where name = 'clubs')
                and id_agenda_segment = (select id from agenda_segment where name = 'clubs_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'avatar_group')
          )
      );


-- Dokumenty --

--Položky
INSERT INTO agenda_segment ("name", source_table, file_source_table) VALUES('club_document', 'agenda_values', 'agenda_has_file');

INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('club_document', true, NULL);

--agenda_has_segment_definition
INSERT INTO agenda_has_segment_definition (id_agenda, id_agenda_segment, priority, is_main)
VALUES(
          (select id from agenda where name = 'clubs')
      , (select id from agenda_segment where name = 'club_document')
      , 5, false
      );

--agenda_has_segment_has_attribute_group_definition
INSERT INTO agenda_has_segment_has_attribute_group_definition (id_agenda_has_segment_definition, id_agenda_attribute_group, is_read_only, priority)
VALUES(
          (select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'clubs')
             and id_agenda_segment = (select id from agenda_segment where name = 'club_document')
          )
      , (select id from agenda_attribute_group where name = 'club_document')
      , false, 1
      );

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'name')
      , 10, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
             where id_agenda = (select id from agenda where name = 'clubs')
            and id_agenda_segment = (select id from agenda_segment where name = 'club_document')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_document')
          )
      );

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'dt')
      , 20, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
            select id from agenda_has_segment_definition
              where id_agenda = (select id from agenda where name = 'clubs')
               and id_agenda_segment = (select id from agenda_segment where name = 'club_document')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_document')
          )
      );

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
       (select id from attribute where name = 'attached_files')
      , 30, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
            select id from agenda_has_segment_definition
              where id_agenda = (select id from agenda where name = 'clubs')
                and id_agenda_segment = (select id from agenda_segment where name = 'club_document')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_document')
          )
      );


-- Statutáři --

--Položky
INSERT INTO agenda_segment ("name", source_table, file_source_table) VALUES('club_statutory', 'agenda_values', 'agenda_has_file');

INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('club_statutory', true, NULL);

--agenda_has_segment_definition
INSERT INTO agenda_has_segment_definition (id_agenda, id_agenda_segment, priority, is_main)
VALUES(
          (select id from agenda where name = 'clubs')
      , (select id from agenda_segment where name = 'club_statutory')
      , 5, false
      );

--agenda_has_segment_has_attribute_group_definition
INSERT INTO agenda_has_segment_has_attribute_group_definition (id_agenda_has_segment_definition, id_agenda_attribute_group, is_read_only, priority)
VALUES(
          (select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'clubs')
             and id_agenda_segment = (select id from agenda_segment where name = 'club_statutory')
          )
      , (select id from agenda_attribute_group where name = 'club_statutory')
      , false, 1
      );

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('statutory_from', 'Členství od', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'date'));

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'statutory_from')
      , 10, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
             where id_agenda = (select id from agenda where name = 'clubs')
            and id_agenda_segment = (select id from agenda_segment where name = 'club_statutory')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_statutory')
          )
      );

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('statutory_function', 'Funkce', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'string'));

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'statutory_function')
      , 20, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
             where id_agenda = (select id from agenda where name = 'clubs')
            and id_agenda_segment = (select id from agenda_segment where name = 'club_statutory')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_statutory')
          )
      );

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'firstname')
      , 30, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
             where id_agenda = (select id from agenda where name = 'clubs')
            and id_agenda_segment = (select id from agenda_segment where name = 'club_statutory')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_statutory')
          )
      );

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'surname')
      , 40, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
             where id_agenda = (select id from agenda where name = 'clubs')
            and id_agenda_segment = (select id from agenda_segment where name = 'club_statutory')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_statutory')
          )
      );

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'dt_birthdate')
      , 50, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
             where id_agenda = (select id from agenda where name = 'clubs')
            and id_agenda_segment = (select id from agenda_segment where name = 'club_statutory')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_statutory')
          )
      );

--
--Adresa - Statutáři
--

--Ulice
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'street')
      , 60, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition
            where id_agenda = (select id from agenda where name = 'clubs')
            and id_agenda_segment = (select id from agenda_segment where name = 'club_statutory')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'club_statutory')
          )
);

--Číslo popisné
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'street_number')
      , 70, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition
                                                   where id_agenda = (select id from agenda where name = 'clubs')
                                                     and id_agenda_segment = (select id from agenda_segment where name = 'club_statutory')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'club_statutory')
          )
      );

--Číslo orientační
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'house_number')
      , 80, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition
                                                   where id_agenda = (select id from agenda where name = 'clubs')
                                                     and id_agenda_segment = (select id from agenda_segment where name = 'club_statutory')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'club_statutory')
          )
      );

--Městská část
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'city_area')
      , 90, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition
                                                   where id_agenda = (select id from agenda where name = 'clubs')
                                                     and id_agenda_segment = (select id from agenda_segment where name = 'club_statutory')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'club_statutory')
          )
      );

--Město / obec
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'city')
      , 100, false, NULL, false, false, NULL, 3, 0, 1, true, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition
                                                   where id_agenda = (select id from agenda where name = 'clubs')
                                                     and id_agenda_segment = (select id from agenda_segment where name = 'club_statutory')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'club_statutory')
          )
      );

--PSČ
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'postal_code')
      , 110, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition
                                                   where id_agenda = (select id from agenda where name = 'clubs')
                                                     and id_agenda_segment = (select id from agenda_segment where name = 'club_statutory')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'club_statutory')
          )
      );

--Kraj
-- INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
--                               , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
--                               , in_title_and_priority, id_agenda_segment_attribute_group_definition)
-- VALUES(
--           (select id from attribute where name = 'address_region')
--       , 120, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
--       , (select id from agenda_has_segment_has_attribute_group_definition
--          where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition
--                                                    where id_agenda = (select id from agenda where name = 'clubs')
--                                                      and id_agenda_segment = (select id from agenda_segment where name = 'club_statutory')
--          )
--            and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'club_statutory')
--           )
--       );

--Země
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'country')
      , 130, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition
                                                   where id_agenda = (select id from agenda where name = 'clubs')
                                                     and id_agenda_segment = (select id from agenda_segment where name = 'club_statutory')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'club_statutory')
          )
      );


-- Datum objednávky --
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('member_fee_order_date', 'Datum objednávky', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'date'));


INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'member_fee_order_date')
      , 30, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
            select id from agenda_has_segment_definition
                      where id_agenda = (select id from agenda where name = 'members')
                      and id_agenda_segment = (select id from agenda_segment where name = 'members_fee')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'members_fee')
          )
      );


-- remove name of honor --
-- check agenda_values
-- select * from agenda_values
--     where id_agenda_definition = (
--      SELECT id FROM mv_agenda_definition
--         WHERE agenda_name = 'members'
--         AND segment_name = 'honors'
--         AND attribute_group_name = 'honors'
--         AND attribute_name = 'honor_name'
-- );

-- remove all value from agenda_values with definition --
delete from agenda_values
    where id_agenda_definition = (
     SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'members'
        AND segment_name = 'honors'
        AND attribute_group_name = 'honors'
        AND attribute_name = 'honor_name'
);

-- remove agenda_definition --
delete from agenda_definition
WHERE id = (
     SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'members'
        AND segment_name = 'honors'
        AND attribute_group_name = 'honors'
        AND attribute_name = 'honor_name'
);

-- Vyznamenání/honors - rename --
update agenda_segment set name = 'honor_regional' where name = 'honors';
update agenda_attribute_group set name = 'honor_regional' where name = 'honors';


-- Úroveň vyznamenání --
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('level_of_honor', 'Úroveň vyznamenání', 0, 'lookup_level_of_honor', NULL, 1, (select id from attribute_data_type where name = 'lookup'));

INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_level_of_honor', 'bronze', 'Bronzové', 10);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_level_of_honor', 'silver', 'Stříbrné', 20);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_level_of_honor', 'gold', 'Zlaté', 30);

-- vyzmanenání krajské --
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'level_of_honor')
      , 10, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
            select id from agenda_has_segment_definition
              where id_agenda = (select id from agenda where name = 'members')
                and id_agenda_segment = (select id from agenda_segment where name = 'honor_regional')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'honor_regional')
          )
      );

-- Vyznamenání - celosvazové --

--Položky
INSERT INTO agenda_segment ("name", source_table, file_source_table) VALUES('honor_union', 'agenda_values', 'agenda_has_file');

INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('honor_union', true, NULL);

--agenda_has_segment_definition
INSERT INTO agenda_has_segment_definition (id_agenda, id_agenda_segment, priority, is_main)
VALUES(
      (select id from agenda where name = 'members')
      , (select id from agenda_segment where name = 'honor_union')
      , 1, false
      );

--agenda_has_segment_has_attribute_group_definition
INSERT INTO agenda_has_segment_has_attribute_group_definition (id_agenda_has_segment_definition, id_agenda_attribute_group, is_read_only, priority)
VALUES(
          (select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'members')
             and id_agenda_segment = (select id from agenda_segment where name = 'honor_union')
          )
      , (select id from agenda_attribute_group where name = 'honor_union')
      , false, 1
      );

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'level_of_honor')
      , 10, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
            select id from agenda_has_segment_definition
              where id_agenda = (select id from agenda where name = 'members')
                and id_agenda_segment = (select id from agenda_segment where name = 'honor_union')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'honor_union')
          )
      );

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'dt_granted')
      , 20, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
            select id from agenda_has_segment_definition
            where id_agenda = (select id from agenda where name = 'members')
             and id_agenda_segment = (select id from agenda_segment where name = 'honor_union')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'honor_union')
          )
      );

-- segment tab position

update agenda_has_segment_definition set priority = 20
where id_agenda = (select id from agenda where name  = 'members')
AND id_agenda_segment = (select id from agenda_segment where name  = 'members_fee');

update agenda_has_segment_definition set priority = 30
where id_agenda = (select id from agenda where name  = 'members')
AND id_agenda_segment = (select id from agenda_segment where name  = 'kyu');

update agenda_has_segment_definition set priority = 40
where id_agenda = (select id from agenda where name  = 'members')
AND id_agenda_segment = (select id from agenda_segment where name  = 'dan');

update agenda_has_segment_definition set priority = 50
where id_agenda = (select id from agenda where name  = 'members')
AND id_agenda_segment = (select id from agenda_segment where name  = 'coach');

update agenda_has_segment_definition set priority = 60
where id_agenda = (select id from agenda where name  = 'members')
AND id_agenda_segment = (select id from agenda_segment where name  = 'examiner');

update agenda_has_segment_definition set priority = 70
where id_agenda = (select id from agenda where name  = 'members')
AND id_agenda_segment = (select id from agenda_segment where name  = 'referee');

update agenda_has_segment_definition set priority = 80
where id_agenda = (select id from agenda where name  = 'members')
AND id_agenda_segment = (select id from agenda_segment where name  = 'honor_regional');

update agenda_has_segment_definition set priority = 90
where id_agenda = (select id from agenda where name  = 'members')
AND id_agenda_segment = (select id from agenda_segment where name  = 'honor_union');

update agenda_has_segment_definition set priority = 100
where id_agenda = (select id from agenda where name  = 'members')
AND id_agenda_segment = (select id from agenda_segment where name  = 'member_attachments');

-- member is search by
update agenda_definition set is_search_by = false
where id = (
 SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'members_main'
    AND attribute_name = 'foreigner'
);

update agenda_definition set is_search_by = false
where id = (
 SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'members_main'
    AND attribute_name = 'gender'
);

--Městská část
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'city_area')
      , 45, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
            select id from agenda_has_segment_definition
               where id_agenda = (select id from agenda where name = 'clubs')
                and id_agenda_segment = (select id from agenda_segment where name = 'clubs_address')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'clubs_address_postal')
          )
      );

-----------------------------------
-- Run only AFTER full migration --
----------- #13728 ----------------
-- DROP SEQUENCE IF EXISTS flexii.seq_member_id CASCADE;
CREATE SEQUENCE flexii.seq_club_id
    INCREMENT BY 1
    MINVALUE 0
    MAXVALUE 2147483647
    START WITH 1
    CACHE 1
    NO CYCLE
    OWNED BY NONE;
-- ddl-end --
ALTER SEQUENCE flexii.seq_club_id OWNER TO flexii;

UPDATE attribute SET custom_data_class = 'ClubId' WHERE name = 'id_club';

UPDATE agenda_definition SET is_disabled = TRUE
WHERE id = (
SELECT id FROM mv_agenda_definition
  WHERE agenda_name = 'clubs'
    AND segment_name = 'clubs_main'
    AND attribute_group_name = 'clubs_main'
    AND attribute_name = 'id_club'
);

-- file specification member_attachments --
UPDATE agenda_definition
    SET file_specification = 'doc,docx,xls,xlsx,ppt,pptx,pdf,txt,odt,odp,ods,csv,xml,html,htm,eps,rtf,log,md,json,jpg,jpeg,png,gif,heic'
WHERE 1=1
AND id = (
SELECT id FROM mv_agenda_definition
  WHERE agenda_name = 'members'
    AND segment_name = 'member_attachments'
    AND attribute_group_name = 'member_attachments'
    AND attribute_name = 'attached_files'
);

-- file specification club_document --
UPDATE agenda_definition
    SET file_specification = 'doc,docx,xls,xlsx,ppt,pptx,pdf,txt,odt,odp,ods,csv,xml,html,htm,eps,rtf,log,md,json,jpg,jpeg,png,gif,heic'
WHERE 1=1
AND id = (
SELECT id FROM mv_agenda_definition
  WHERE agenda_name = 'clubs'
    AND segment_name = 'club_document'
    AND attribute_group_name = 'club_document'
    AND attribute_name = 'attached_files'
);
-- #13695 update weight_tolerance --
update flexii.lookup_categories set weight_tolerance = 0.2
where name in ('2023_u8', '2023_u10', '2023_mu12', '2023_mu14', '2023_mu16', '2023_mu18', '2023_wu12', '2023_wu14', '2023_wu16', '2023_wu18');

alter table flexii.lookup_categories
    alter column weight_tolerance set default 0.2;

-- #13796 --
REFRESH MATERIALIZED VIEW mv_agenda_definition;

update agenda_definition set is_not_null = false
where id in (
    SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'clubs'
    AND segment_name = 'clubs_main'
    AND attribute_group_name = 'avatar_group'
    AND attribute_name = 'avatar'
);

-- #13797 --
update agenda_definition set priority = 37
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournaments_main'
    AND attribute_group_name = 'tournaments_main'
    AND attribute_name = 'tournament_type'
);

-- #13636 --
update agenda_definition set is_unique = false
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'clubs'
    AND segment_name = 'clubs_main'
    AND attribute_group_name = 'clubs_main'
    AND attribute_name = 'dt_of_change'
);

update agenda_definition set is_unique = true
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'clubs'
    AND segment_name = 'clubs_main'
    AND attribute_group_name = 'clubs_main'
    AND attribute_name IN ('identifier_idnumber', 'taxnumber', 'account_number', 'club_hajime_acronym')
);

update agenda_definition set priority = 55
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'clubs'
    AND segment_name = 'clubs_main'
    AND attribute_group_name = 'clubs_main'
    AND attribute_name = 'taxnumber'
);

update agenda_definition set priority = 60
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'clubs'
    AND segment_name = 'clubs_main'
    AND attribute_group_name = 'clubs_main'
    AND attribute_name = 'entry_register_of_associations'
);

update agenda_definition set priority = 62
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'clubs'
    AND segment_name = 'clubs_main'
    AND attribute_group_name = 'clubs_main'
    AND attribute_name = 'account_number'
);

----------------- #13757 -----------------
-- Zaplacen čl. popl. na tento rok --
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('member_fee_current_year', 'Zaplacen čl. popl. na tento rok', 0, 'lookup_bool', NULL, 1, (select id from attribute_data_type where name = 'lookup'));

INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_bool', 'false', 'Ne', 10);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_bool', 'true', 'Ano', 20);


INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'member_fee_current_year')
      , 97, false, NULL, false, true, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
             select id from agenda_has_segment_definition
             where id_agenda = (select id from agenda where name = 'members')
               and id_agenda_segment = (select id from agenda_segment where name = 'members_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'members_main')
          )
      );

INSERT INTO agenda_has_custom_class (id_agenda, custom_class, is_on_create, is_on_edit, is_on_delete, is_on_load)
VALUES((SELECT id FROM agenda WHERE name = 'members'), 'MemberFee', true, true, false, false);

------ #12623 ------
-- revert changes --

----
--  Minimální Kyu
----

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES ('min_kyu_level', 'Minimální Kyu', 0, 'lookup_kyu_level', NULL, 1,
        (select id from attribute_data_type where name = 'lookup'));

INSERT INTO agenda_definition ( id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height
                              , is_in_datagrid, is_search_by, in_title_and_priority, is_unique
                              , id_agenda_segment_attribute_group_definition)
VALUES ( (select id from attribute where name = 'min_kyu_level')
       , 120, false, NULL, false, false, NULL, 3, 0, 1, false, true, null, false
       , (select id
          from agenda_has_segment_has_attribute_group_definition
          where id_agenda_has_segment_definition = (
            select id
            from agenda_has_segment_definition
            where id_agenda = (select id from agenda where name = 'tournaments')
              and id_agenda_segment =
                  (select id from agenda_segment where name = 'tournaments_main'))
            and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournaments_main'))
       );


----
--  MEZINÁRODNÍ TURNAJE
----

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES ('international_tournament', 'Mezinárodní turnaj', 0, 'lookup_bool', NULL, 1,
        (select id from attribute_data_type where name = 'lookup'));


INSERT INTO agenda_definition ( id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height
                              , is_in_datagrid, is_search_by, in_title_and_priority, is_unique
                              , id_agenda_segment_attribute_group_definition)
VALUES ( (select id from attribute where name = 'international_tournament')
       , 75, true, (SELECT id FROM master_lookup WHERE enabled = true AND lookup_name = 'lookup_bool' AND name = 'false'), false, false, NULL, 3, 0, 1, true, true, null, false
       , (select id
          from agenda_has_segment_has_attribute_group_definition
          where id_agenda_has_segment_definition = (
            select id
            from agenda_has_segment_definition
            where id_agenda = (select id from agenda where name = 'tournaments')
              and id_agenda_segment =
                  (select id from agenda_segment where name = 'tournaments_main'))
          and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournaments_main'))
       );


----
--  Zápočet do ranking listu
----
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES ('ranking_list_inclusion', 'Zápočet do ranking listu', 0, 'lookup_bool', NULL, 1,
        (select id from attribute_data_type where name = 'lookup'));


INSERT INTO agenda_definition ( id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height
                              , is_in_datagrid, is_search_by, in_title_and_priority, is_unique
                              , id_agenda_segment_attribute_group_definition)
VALUES ( (select id from attribute where name = 'ranking_list_inclusion')
       , 140, true, (SELECT id FROM master_lookup WHERE enabled = true AND lookup_name = 'lookup_bool' AND name = 'false'), false, false, NULL, 3, 0, 1, false, true, null, false
       , (select id
          from agenda_has_segment_has_attribute_group_definition
          where id_agenda_has_segment_definition = (
            select id
            from agenda_has_segment_definition
            where id_agenda = (select id from agenda where name = 'tournaments')
              and id_agenda_segment =
                  (select id from agenda_segment where name = 'tournaments_main'))
            and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournaments_main'))
       );


--------------------
--  Stav turnaje  --
--------------------

INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION)
VALUES ('lookup_tournament_status', 'status_1', 'Plánovaný');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION)
VALUES ('lookup_tournament_status', 'status_2', 'Spuštěný');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION)
VALUES ('lookup_tournament_status', 'status_3', 'Ukončený');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION)
VALUES ('lookup_tournament_status', 'status_4', 'Zrušený');


INSERT INTO agenda_definition ( id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height
                              , is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES ( (select id from attribute where name = 'tournament_status')
       , 17, false, (SELECT id FROM master_lookup WHERE enabled = true AND lookup_name = 'lookup_tournament_status' AND name = 'status_1'), false, false, NULL, 3, 0, 1, true, true, NULL
       , (select id
          from agenda_has_segment_has_attribute_group_definition
          where id_agenda_has_segment_definition = (
            select id
            from agenda_has_segment_definition
            where id_agenda = (select id from agenda where name = 'tournaments')
              and id_agenda_segment =
                  (select id from agenda_segment where name = 'tournaments_main'))
            and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournaments_main'))
       );

refresh materialized view mv_agenda_definition;

update agenda_definition set priority = 37
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournaments_main'
    AND attribute_group_name = 'tournaments_main'
    AND attribute_name = 'web_link_for_registration'
);

update agenda_definition set priority = 15
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournaments_main'
    AND attribute_group_name = 'tournaments_main'
    AND attribute_name = 'tournament_type'
);

update agenda_definition set is_not_null = true
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournaments_main'
    AND attribute_group_name = 'tournaments_main'
    AND attribute_name = 'tournament_status'
);

update agenda_definition set is_read_only = true
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'members_main'
    AND attribute_name = 'dt_birthyear'
);

--- #13631 changes ---
DELETE FROM agenda_values WHERE id_agenda_definition IN (
    SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'members'
        AND segment_name = 'members_main'
        AND attribute_group_name = 'members_main'
        AND attribute_name = 'identifier_type'
);

DELETE FROM agenda_definition where id in (
    SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'members'
        AND segment_name = 'members_main'
        AND attribute_group_name = 'members_main'
        AND attribute_name = 'identifier_type'
);

DELETE FROM master_lookup WHERE lookup_name = 'lookup_identifier_type';

DELETE FROM attribute WHERE name = 'identifier_type';

update attribute set description = 'Rodné číslo'
where name = 'identifier_value';

update agenda_definition set is_not_null = false, priority = 50
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'members_main'
    AND attribute_name = 'identifier_value'
);


----
--  Náhradní identifikátor
----
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('alternate_identifier', 'Náhradní identifikátor/pas', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'string'));

INSERT INTO agenda_definition ( id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height
                              , is_in_datagrid, is_search_by, in_title_and_priority, is_unique
                              , id_agenda_segment_attribute_group_definition)
VALUES ( (select id from attribute where name = 'alternate_identifier')
       , 55, false, NULL, false, false, NULL, 3, 0, 1, false, true, null, false
       , (select id
          from agenda_has_segment_has_attribute_group_definition
          where id_agenda_has_segment_definition = (
            select id
            from agenda_has_segment_definition
            where id_agenda = (select id from agenda where name = 'members')
              and id_agenda_segment =
                  (select id from agenda_segment where name = 'members_main'))
            and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'members_main'))
       );


----
--  Závislá povinnost, pokud je cizinec, schovat pole rodné číslo a nastavit povinnost na "Náhradní identifikátor"
----

INSERT INTO agenda_definition_specification_by_value
(id_agenda_definition, id_agenda_definition_control, value_control, is_hidden, is_not_null, default_value, is_disabled,
 is_read_only)
VALUES ((SELECT id
         FROM agenda_definition
         WHERE enabled = true
           AND id_attribute = (SELECT id FROM attribute WHERE name = 'identifier_value')
           AND id_agenda_segment_attribute_group_definition = (
           select id
           from agenda_has_segment_has_attribute_group_definition
           where id_agenda_has_segment_definition = (
                select id from agenda_has_segment_definition
                where id_agenda = (select id from agenda where name = 'members')
                and id_agenda_segment = (select id from agenda_segment where name = 'members_main')
           )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'members_main')))
        ,
        (SELECT id
         FROM agenda_definition
         WHERE enabled = true
           AND id_attribute = (SELECT id FROM attribute WHERE name = 'foreigner')
           AND id_agenda_segment_attribute_group_definition = (
           select id from agenda_has_segment_has_attribute_group_definition
           where id_agenda_has_segment_definition = (
               select id from agenda_has_segment_definition
                 where id_agenda = (select id from agenda where name = 'members')
                   and id_agenda_segment = (select id from agenda_segment where name = 'members_main')
           )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'members_main'))),
        1, true, false, NULL, false, false);

INSERT INTO agenda_definition_specification_by_value
(id_agenda_definition, id_agenda_definition_control, value_control, is_hidden, is_not_null, default_value, is_disabled,
 is_read_only)
VALUES ((SELECT id
         FROM agenda_definition
         WHERE enabled = true
           AND id_attribute = (SELECT id FROM attribute WHERE name = 'alternate_identifier')
           AND id_agenda_segment_attribute_group_definition = (
           select id
           from agenda_has_segment_has_attribute_group_definition
           where id_agenda_has_segment_definition = (
                select id from agenda_has_segment_definition
                where id_agenda = (select id from agenda where name = 'members')
                and id_agenda_segment = (select id from agenda_segment where name = 'members_main')
           )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'members_main')))
        ,
        (SELECT id
         FROM agenda_definition
         WHERE enabled = true
           AND id_attribute = (SELECT id FROM attribute WHERE name = 'foreigner')
           AND id_agenda_segment_attribute_group_definition = (
           select id from agenda_has_segment_has_attribute_group_definition
           where id_agenda_has_segment_definition = (
               select id from agenda_has_segment_definition
                 where id_agenda = (select id from agenda where name = 'members')
                   and id_agenda_segment = (select id from agenda_segment where name = 'members_main')
           )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'members_main'))),
        1, false, true, NULL, false, false);

----
--  Závislá povinnost, pokud není cizinec, nastavit povinnost na "Rodné číslo"
----

INSERT INTO agenda_definition_specification_by_value
(id_agenda_definition, id_agenda_definition_control, value_control, is_hidden, is_not_null, default_value, is_disabled,
 is_read_only)
VALUES ((SELECT id
         FROM agenda_definition
         WHERE enabled = true
           AND id_attribute = (SELECT id FROM attribute WHERE name = 'identifier_value')
           AND id_agenda_segment_attribute_group_definition = (
           select id
           from agenda_has_segment_has_attribute_group_definition
           where id_agenda_has_segment_definition = (
                select id from agenda_has_segment_definition
                where id_agenda = (select id from agenda where name = 'members')
                and id_agenda_segment = (select id from agenda_segment where name = 'members_main')
           )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'members_main')))
        ,
        (SELECT id
         FROM agenda_definition
         WHERE enabled = true
           AND id_attribute = (SELECT id FROM attribute WHERE name = 'foreigner')
           AND id_agenda_segment_attribute_group_definition = (
           select id from agenda_has_segment_has_attribute_group_definition
           where id_agenda_has_segment_definition = (
               select id from agenda_has_segment_definition
                 where id_agenda = (select id from agenda where name = 'members')
                   and id_agenda_segment = (select id from agenda_segment where name = 'members_main')
           )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'members_main'))),
        0, false, true, NULL, false, false);

-- Členové - zákonný zástupce2
INSERT INTO agenda_has_agenda (name, id_agenda, id_agenda_linked, relation, is_unique, priority, is_quick_add_link, is_quick_add_link_linked, is_quick_add_agenda, is_quick_add_agenda_linked, is_custom_name)
VALUES (
           'members_has_parents2'
       , (select id from agenda where name = 'members')
       , (select id from agenda where name = 'parents')
       , 2, false, 180, false, false, false, false, true);

--Zákonný zástupce
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('members_has_parents2', 'Zákonný zástupce 2', 0, 'members_has_parents2', NULL, 1, (select id from attribute_data_type where name = 'agenda_has_agenda'));


--ID
INSERT INTO agenda_has_agenda_show_attribute (id_agenda_has_agenda, id_agenda_definition, is_hidden_in_datagrid) VALUES(
(select id from agenda_has_agenda where name = 'members_has_parents2')
, (select id from agenda_definition where id_attribute = (select id from attribute where name = 'id_parent')
      AND id_agenda_segment_attribute_group_definition = (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'parents')
                                                                                                  and id_agenda_segment = (select id from agenda_segment where name = 'parents_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'parents_main')
      )
     )
, false);

--Jméno
INSERT INTO agenda_has_agenda_show_attribute (id_agenda_has_agenda, id_agenda_definition, is_hidden_in_datagrid) VALUES(
(select id from agenda_has_agenda where name = 'members_has_parents2')
, (select id from agenda_definition where id_attribute = (select id from attribute where name = 'firstname')
      AND id_agenda_segment_attribute_group_definition = (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'parents')
                                                                                                  and id_agenda_segment = (select id from agenda_segment where name = 'parents_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'parents_main')
      )
     )
, false);

--Příjmení
INSERT INTO agenda_has_agenda_show_attribute (id_agenda_has_agenda, id_agenda_definition, is_hidden_in_datagrid) VALUES(
(select id from agenda_has_agenda where name = 'members_has_parents2')
, (select id from agenda_definition where id_attribute = (select id from attribute where name = 'surname')
      AND id_agenda_segment_attribute_group_definition = (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'parents')
                                                                                                  and id_agenda_segment = (select id from agenda_segment where name = 'parents_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'parents_main')
      )
     )
, false);

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
       (select id from attribute where name = 'members_has_parents2')
      , 74, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
            select id from agenda_has_segment_definition
            where id_agenda = (select id from agenda where name = 'members')
            and id_agenda_segment = (select id from agenda_segment where name = 'members_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'members_main')
          )
      );

REFRESH MATERIALIZED VIEW mv_agenda_definition;

update agenda_definition set priority = 65
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'members_main'
    AND attribute_name = 'dt_of_change'
);

update agenda_definition set priority = 70
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'members_main'
    AND attribute_name = 'nonage'
);

update agenda_definition set priority = 72
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'members_main'
    AND attribute_name = 'members_has_parents'
);

update agenda_definition set priority = 76
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'members_main'
    AND attribute_name = 'active_fighter'
);

-- Odstranění aha  competitions_has_members (Funkcionáři soutěže) --

-- check if aha has values --
-- select * from agenda_has_agenda_values where id_agenda_has_agenda_definition in (
--     select id from agenda_has_agenda_definition where id_agenda_has_agenda = (
--         select id from agenda_has_agenda where name =  'competitions_has_members'
--     )
-- );

DELETE FROM agenda_has_agenda_definition where id_agenda_has_agenda = (
    select id from agenda_has_agenda where name =  'competitions_has_members'
);

DELETE FROM attribute WHERE name = 'competitions_has_members';

DELETE FROM agenda_has_agenda_show_attribute
 where id_agenda_has_agenda = (
    select id from agenda_has_agenda where name = 'competitions_has_members'
);

DELETE FROM agenda_has_agenda WHERE name = 'competitions_has_members';

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- hosting aha changes --

update agenda_has_agenda set id_agenda_linked = (select id from agenda where name = 'clubs'), name = 'clubs_has_members_hosting', is_unique = false
where name = 'competition_has_members_hosting';

-- new attribute --
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('agenda_competitions', 'Soutěž', 0, 'competitions', NULL, 1, (select id from attribute_data_type where name = 'agenda'));

update agenda_has_agenda_definition set id_attribute = (select id from attribute where name = 'agenda_competitions')
where id = (
    select id from agenda_has_agenda_definition
    where id_agenda_has_agenda in (
        select id from agenda_has_agenda where name = 'clubs_has_members_hosting'
    ) AND id_attribute = (select id from attribute where name = 'agenda_clubs')
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

delete from agenda_has_agenda_show_attribute
where id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_members_hosting')
AND id_agenda_definition = (
    SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'members'
        AND segment_name = 'members_main'
        AND attribute_group_name = 'members_main'
        AND attribute_name = 'id_member'
);



------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Adminisrátor turnaje
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-- menu
INSERT INTO agenda  (name, icon, is_importable) VALUES('tournament_administrator', 'fad fa-user-cog', false);

INSERT INTO menu_link (name, presenter, action, parameters) VALUES('tournament_administrator_default', 'Agenda:Agenda', 'default', '{"name":"tournament_administrator"}');
INSERT INTO menu_link (name, presenter, action, parameters) VALUES('tournament_administrator_detail', 'Agenda:Agenda', 'detail', '{"name":"tournament_administrator"}');

INSERT INTO menu_item (id_menu_section, id_agenda, name, icon, id_menu_link, priority, is_default)
VALUES(
          (select id from menu_section where name = 'tournaments')
      , (select id from agenda where name = 'tournament_administrator')
      , 'tournament_administrator', 'fad fa-user-cog'
      , (select id from menu_link where name = 'tournament_administrator_default'), 10, false);

INSERT INTO menu_item_has_menu_link (id_menu_item, id_menu_link)
VALUES(
          (select id from menu_item where name = 'tournament_administrator')
      , (select id from menu_link where name = 'tournament_administrator_detail')
      );


INSERT INTO agenda_segment ("name", source_table, file_source_table) VALUES('tournament_administrator_main', 'agenda_values', 'agenda_has_file');


--tournament_administrator_main
INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('tournament_administrator_main', false, NULL);

--agenda_has_segment_definition
INSERT INTO agenda_has_segment_definition (id_agenda, id_agenda_segment, priority, is_main)
VALUES(
          (select id from agenda where name = 'tournament_administrator')
      , (select id from agenda_segment where name = 'tournament_administrator_main')
      , 1, true
      );

--agenda_has_segment_has_attribute_group_definition
INSERT INTO agenda_has_segment_has_attribute_group_definition (id_agenda_has_segment_definition, id_agenda_attribute_group, is_read_only, priority)
VALUES(
          (select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'tournament_administrator')
             and id_agenda_segment = (select id from agenda_segment where name = 'tournament_administrator_main')
          )
      , (select id from agenda_attribute_group where name = 'tournament_administrator_main')
      , false, 1
      );


--Appuser
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('appuser', 'Uživatel', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'appuser'));

--Uživatel
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'appuser')
      , 5, true, NULL, false, false, NULL, 3, 0, 1, true, true, 1, true, true
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'tournament_administrator')
            and id_agenda_segment = (select id from agenda_segment where name = 'tournament_administrator_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_administrator_main')
          )
      );

--Člen
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('member', 'Člen', 0, 'members', NULL, 1, (select id from attribute_data_type where name = 'agenda'));

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'member')
      , 10, true, NULL, false, false, NULL, 3, 0, 1, true, false, NULL, true, true
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'tournament_administrator')
            and id_agenda_segment = (select id from agenda_segment where name = 'tournament_administrator_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_administrator_main')
          )
      );

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'email')
      , 15, true, NULL, false, false, NULL, 3, 0, 1, true, false, null, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'tournament_administrator')
            and id_agenda_segment = (select id from agenda_segment where name = 'tournament_administrator_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_administrator_main')
          )
      );

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'telephone')
      , 20, false, NULL, false, false, NULL, 3, 0, 1, false, false, null, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'tournament_administrator')
            and id_agenda_segment = (select id from agenda_segment where name = 'tournament_administrator_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_administrator_main')
          )
      );

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'is_active')
      , 25, true, 1, false, false, NULL, 3, 0, 1, true, false, null, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'tournament_administrator')
            and id_agenda_segment = (select id from agenda_segment where name = 'tournament_administrator_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_administrator_main')
          )
      );


-- tournament_administrator - torunament
INSERT INTO agenda_has_agenda (name, id_agenda, id_agenda_linked, relation, is_unique, priority, is_quick_add_link, is_quick_add_link_linked, is_quick_add_agenda, is_quick_add_agenda_linked, is_custom_name)
VALUES (
       'tournaments_has_tournament_administrator'
       , (select id from agenda where name = 'tournaments')
       , (select id from agenda where name = 'tournament_administrator')
       , 2, false, 190, false, false, false, false, true);


--Datum konání od
INSERT INTO agenda_has_agenda_show_attribute (id_agenda_has_agenda, id_agenda_definition, is_hidden_in_datagrid) VALUES(
(select id from agenda_has_agenda where name = 'tournaments_has_tournament_administrator')
, (select id from agenda_definition where id_attribute = (select id from attribute where name = 'dt_tournament_start')
      AND id_agenda_segment_attribute_group_definition = (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'tournaments')
           and id_agenda_segment = (select id from agenda_segment where name = 'tournaments_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournaments_main')
      )
     )
, false);

--Datum konání do
INSERT INTO agenda_has_agenda_show_attribute (id_agenda_has_agenda, id_agenda_definition, is_hidden_in_datagrid) VALUES(
(select id from agenda_has_agenda where name = 'tournaments_has_tournament_administrator')
, (select id from agenda_definition where id_attribute = (select id from attribute where name = 'dt_tournament_end')
      AND id_agenda_segment_attribute_group_definition = (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
            where id_agenda = (select id from agenda where name = 'tournaments')
            and id_agenda_segment = (select id from agenda_segment where name = 'tournaments_main')
         )
         and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournaments_main')
      )
     )
, false);

-- Lokace
INSERT INTO agenda_has_agenda_show_attribute (id_agenda_has_agenda, id_agenda_definition, is_hidden_in_datagrid) VALUES(
(select id from agenda_has_agenda where name = 'tournaments_has_tournament_administrator')
, (select id from agenda_definition where id_attribute = (select id from attribute where name = 'locations')
      AND id_agenda_segment_attribute_group_definition = (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
            where id_agenda = (select id from agenda where name = 'tournaments')
            and id_agenda_segment = (select id from agenda_segment where name = 'tournaments_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournaments_main')
      )
     )
, false);

-- Stav
INSERT INTO agenda_has_agenda_show_attribute (id_agenda_has_agenda, id_agenda_definition, is_hidden_in_datagrid) VALUES(
(select id from agenda_has_agenda where name = 'tournaments_has_tournament_administrator')
, (select id from agenda_definition where id_attribute = (select id from attribute where name = 'tournament_status')
      AND id_agenda_segment_attribute_group_definition = (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'tournaments')
           and id_agenda_segment = (select id from agenda_segment where name = 'tournaments_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournaments_main')
      )
     )
, false);

-- Administrátor turnaje
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('agenda_tournament_administrator', 'Administrátor turnaje', 0, 'tournaments_has_tournament_administrator', NULL, 1, (select id from attribute_data_type where name = 'agenda_has_agenda'));


INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'agenda_tournament_administrator')
      , 5, false, 1, false, false, NULL, 3, 0, 1, false, false, null, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'tournaments')
            and id_agenda_segment = (select id from agenda_segment where name = 'tournament_information')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_information_main')
          )
      );

REFRESH MATERIALIZED VIEW mv_agenda_definition;

update agenda_definition set field_column_height = 5
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournament_information'
    AND attribute_group_name = 'tournament_information_main'
    AND attribute_name = 'description'
);

-- #12366
UPDATE agenda_values SET value = '1' WHERE id_agenda_definition IN (SELECT ad.id FROM mv_agenda_definition ad JOIN attribute att ON att.id = ad.id_attribute WHERE att.lookup = 'lookup_yes_no') AND value = (SELECT id::varchar from master_lookup WHERE lookup_name = 'lookup_yes_no' AND name = 'yes');
UPDATE agenda_values SET value = '0' WHERE id_agenda_definition IN (SELECT ad.id FROM mv_agenda_definition ad JOIN attribute att ON att.id = ad.id_attribute WHERE att.lookup = 'lookup_yes_no') AND value = (SELECT id::varchar from master_lookup WHERE lookup_name = 'lookup_yes_no' AND name = 'no');

UPDATE attribute SET LOOKUP = null, id_attribute_data_type = (select id from attribute_data_type where name = 'bool') WHERE lookup IN ('lookup_yes_no', 'lookup_bool');


----------------------------------------------------
-- 2.6.0.0
-- SQL for this version ends here
----------------------------------------------------

-- same address changes is_not_null --
update agenda_definition set is_not_null = true
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'clubs'
    AND segment_name = 'clubs_address'
    AND attribute_group_name = 'clubs_address_postal'
    AND attribute_name = 'same_address'
);

-- Members - photo in_not_null --
update agenda_definition set is_not_null = false
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'avatar_group'
    AND attribute_name = 'avatar'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

--- #13633 ---
-- remove all values for definition abbreviation (clubs)
delete from agenda_values where id_agenda_definition in (
     SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'clubs'
        AND segment_name = 'clubs_main'
        AND attribute_group_name = 'clubs_main'
        AND attribute_name = 'abbreviation'
);

-- delete agenda definition
delete from agenda_definition where id in (
    SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'clubs'
        AND segment_name = 'clubs_main'
        AND attribute_group_name = 'clubs_main'
        AND attribute_name = 'abbreviation'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- El. přihlašování
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('electronic_application', 'El. přihlašování', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'bool'));

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled, is_read_only,
                               tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid,
                               is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'electronic_application')
      , 160, false, 1, false, false, NULL, 3, 0, 1, false, false, null, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'clubs')
            and id_agenda_segment = (select id from agenda_segment where name = 'clubs_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'clubs_main')
          )
      );

-- Odkaz el. přihlašování
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('electronic_application_url', 'Odkaz el. přihlašování', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'url'));

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled, is_read_only,
                               tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid,
                               is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'electronic_application_url')
      , 170, false, NULL, false, false, NULL, 3, 0, 1, false, false, null, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'clubs')
            and id_agenda_segment = (select id from agenda_segment where name = 'clubs_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'clubs_main')
          )
      );

update agenda_definition set default_value = 0
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'clubs'
    AND segment_name = 'clubs_main'
    AND attribute_group_name = 'clubs_main'
    AND attribute_name = 'foreign_club'
);

update agenda_definition set default_value = null
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'clubs'
    AND segment_name = 'clubs_main'
    AND attribute_group_name = 'clubs_main'
    AND attribute_name = 'entry_register_of_associations'
);

update agenda_definition set default_value = null
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'clubs'
    AND segment_name = 'clubs_main'
    AND attribute_group_name = 'clubs_main'
    AND attribute_name = 'account_number'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

--- #13633 ---
--- Agenda - clubs ---
-- ------------------------------
-- --záložka Stanovy
-- ------------------------------

--Položky Stanovy
INSERT INTO agenda_segment ("name", source_table, file_source_table) VALUES('club_statutes', 'agenda_values', 'agenda_has_file');

INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('club_statutes_main', true, NULL);

--agenda_has_segment_definition
INSERT INTO agenda_has_segment_definition (id_agenda, id_agenda_segment, priority, is_main)
VALUES(
        (select id from agenda where name = 'clubs')
      , (select id from agenda_segment where name = 'club_statutes')
      , 40, false
      );

--agenda_has_segment_has_attribute_group_definition
INSERT INTO agenda_has_segment_has_attribute_group_definition (id_agenda_has_segment_definition, id_agenda_attribute_group, is_read_only, priority)
VALUES(
          (select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'clubs')
             and id_agenda_segment = (select id from agenda_segment where name = 'club_statutes')
          )
      , (select id from agenda_attribute_group where name = 'club_statutes_main')
      , false, 1
      );

--
--Stanovy klubu
--

--Název
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'name')
      , 10, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition
            where id_agenda = (select id from agenda where name = 'clubs')
            and id_agenda_segment = (select id from agenda_segment where name = 'club_statutes')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_statutes_main')
          )
);



INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'dt_date')
      , 20, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition
            where id_agenda = (select id from agenda where name = 'clubs')
            and id_agenda_segment = (select id from agenda_segment where name = 'club_statutes')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_statutes_main')
          )
);

-- Stanovy
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('statutes', 'Stanovy', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'file'));


INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'statutes')
      , 30, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition
            where id_agenda = (select id from agenda where name = 'clubs')
            and id_agenda_segment = (select id from agenda_segment where name = 'club_statutes')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'club_statutes_main')
          )
);

----
--   Pokud je el. přihláška "Ne", nastavit is_hidden na "Odkaz el. přihlašování"
----

INSERT INTO agenda_definition_specification_by_value
(id_agenda_definition, id_agenda_definition_control, value_control, is_hidden, is_not_null, default_value, is_disabled,
 is_read_only)
VALUES ((SELECT id
         FROM agenda_definition
         WHERE enabled = true
           AND id_attribute = (SELECT id FROM attribute WHERE name = 'electronic_application_url')
           AND id_agenda_segment_attribute_group_definition = (
           select id
           from agenda_has_segment_has_attribute_group_definition
           where id_agenda_has_segment_definition = (
                select id from agenda_has_segment_definition
                where id_agenda = (select id from agenda where name = 'clubs')
                and id_agenda_segment = (select id from agenda_segment where name = 'clubs_main')
           )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'clubs_main')))
        ,
        (SELECT id
         FROM agenda_definition
         WHERE enabled = true
           AND id_attribute = (SELECT id FROM attribute WHERE name = 'electronic_application')
           AND id_agenda_segment_attribute_group_definition = (
           select id from agenda_has_segment_has_attribute_group_definition
           where id_agenda_has_segment_definition = (
               select id from agenda_has_segment_definition
                 where id_agenda = (select id from agenda where name = 'clubs')
                   and id_agenda_segment = (select id from agenda_segment where name = 'clubs_main')
           )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'clubs_main'))),
        0, true, false, NULL, false, false);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

--- #13813 ---
-- lookup_locations changes --
delete from agenda_values where id_agenda_definition in (
  select id from mv_agenda_definition where attribute_name = 'locations'
) and value::int in (
    select id from lookup_locations where name in (
       'Albania', 'Andorra', 'Armenia', 'Austria', 'Azerbaijan', 'Belarus', 'Belgium', 'Bulgaria', 'Croatia', 'Cyprus', 'Denmark',
       'Estonia', 'Finland', 'France', 'Germany', 'Hungary', 'Iceland', 'Italy', 'Kazakhstan', 'Latvia', 'Liechtenstein', 'Luxembourg',
       'Macedonia', 'Malta', 'Monaco', 'Netherlands', 'Norway', 'Poland', 'Portugal', 'Russia', 'San Marino', 'Slovak Republic',
       'Slovenia', 'Spain', 'Sweden', 'Switzerland', 'Turkey', 'United Kingdom', 'Other', 'Neuvedeno'
    )
);

delete from lookup_locations
where name in (
   'Albania', 'Andorra', 'Armenia', 'Austria', 'Azerbaijan', 'Belarus', 'Belgium', 'Bulgaria', 'Croatia', 'Cyprus', 'Denmark', 'Estonia', 'Finland',
   'France', 'Germany', 'Hungary', 'Iceland', 'Italy', 'Kazakhstan', 'Latvia', 'Liechtenstein', 'Luxembourg', 'Macedonia', 'Malta', 'Monaco',
   'Netherlands', 'Norway', 'Poland', 'Portugal', 'Russia', 'San Marino', 'Slovak Republic', 'Slovenia', 'Spain', 'Sweden', 'Switzerland', 'Turkey',
   'United Kingdom', 'Other', 'Neuvedeno'
);

update lookup_locations set priority = 10 where name = 'Praha';
update lookup_locations set priority = 20 where name = 'Středočeský kraj';
update lookup_locations set priority = 30 where name = 'Jihočeský kraj';
update lookup_locations set priority = 40 where name = 'Plzeňský kraj';
update lookup_locations set priority = 50 where name = 'Karlovarský kraj';
update lookup_locations set priority = 60 where name = 'Ústecký kraj';
update lookup_locations set priority = 70 where name = 'Liberecký kraj';
update lookup_locations set priority = 80 where name = 'Královéhradecký kraj';
update lookup_locations set priority = 90 where name = 'Pardubický kraj';
update lookup_locations set priority = 100 where name = 'Kraj Vysočina';
update lookup_locations set priority = 110 where name = 'Jihomoravský kraj';
update lookup_locations set priority = 120 where name = 'Olomoucký kraj';
update lookup_locations set priority = 130 where name = 'Moravskoslezský kraj';
update lookup_locations set priority = 140 where name = 'Zlínský kraj';

insert into lookup_locations (internal_id, name, description, priority) values (0, 'Česká republika', 'Česká republika', 150);

-- 13633 --
-- aha show attribute update --
delete from agenda_has_agenda_show_attribute where id_agenda_has_agenda = (
    select id from agenda_has_agenda where name = 'clubs_has_members'
) AND id_agenda_definition = (
  SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'members_main'
    AND attribute_name = 'id_member'
);

-- 13631 --
-- set default value to 0 --
update agenda_definition set default_value = 0
where id = (
 SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'members_main'
    AND attribute_name = 'foreigner'
);

update agenda_definition set default_value = null
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'members_main'
    AND attribute_name = 'identifier_value'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

CREATE SEQUENCE flexii.seq_tournament_id
    INCREMENT BY 1
    MINVALUE 0
    MAXVALUE 2147483647
    START WITH 1
    CACHE 1
    NO CYCLE
    OWNED BY NONE;
-- ddl-end --
ALTER SEQUENCE flexii.seq_tournament_id OWNER TO flexii;

UPDATE attribute SET custom_data_class = 'TournamentId' WHERE name = 'id_tournament';

UPDATE agenda_definition SET is_disabled = TRUE
WHERE id = (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournaments' AND segment_name = 'tournaments_main' AND attribute_group_name = 'tournaments_main' AND attribute_name = 'id_tournament');

-- #13631 agenda datagrid added dt_upd column --
UPDATE agenda set is_dt_upd_in_datagrid = true
  where name = 'members';

-- #13633 agenda clubs edit title and priority --
UPDATE agenda_definition set in_title_and_priority = null
WHERE id IN (
	SELECT id FROM mv_agenda_definition
	    WHERE agenda_name = 'clubs'
	    AND segment_name = 'clubs_main'
	    AND attribute_group_name = 'clubs_main'
	    AND attribute_name = 'foreign_club'
);

-- #12397 competitions is_search_by changes --
UPDATE agenda_definition set is_search_by = false where id in (
select id FROM mv_agenda_definition
	WHERE agenda_name = 'competitions'
	AND segment_name = 'competitions_main'
	AND attribute_group_name = 'competitions_main'
    AND attribute_name in ('competition_basic_category', 'competition_sub_category', 'competition_team_category', 'competition_type_of_subcategory', 'competition_type_of_championship')
);

-- #13633 added dt_upd as column to main datagrid --
UPDATE agenda set is_dt_upd_in_datagrid = true
  where name = 'clubs';

REFRESH MATERIALIZED VIEW mv_agenda_definition;

--- #13813 --
--- tournaments changes ---

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('max_num_of_participants', 'Maximální počet účastníků', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'int'));


INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'max_num_of_participants')
      , 150, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition
            where id_agenda = (select id from agenda where name = 'tournaments')
            and id_agenda_segment = (select id from agenda_segment where name = 'tournaments_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'tournaments_main')
          )
);

UPDATE agenda_definition set default_value = null where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournament_information'
    AND attribute_group_name = 'tournament_information_main'
    AND attribute_name = 'agenda_tournament_administrator'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

--
-- Infomrace o turnaji --
-- Ředitel soutěže --
--

INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('tournament_director_main', false, NULL);


--agenda_has_segment_has_attribute_group_definition
INSERT INTO agenda_has_segment_has_attribute_group_definition (id_agenda_has_segment_definition, id_agenda_attribute_group, is_read_only, priority)
VALUES(
          (select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'tournaments')
             and id_agenda_segment = (select id from agenda_segment where name = 'tournament_information')
          )
      , (select id from agenda_attribute_group where name = 'tournament_director_main')
      , false, 1
      );

update agenda_has_segment_has_attribute_group_definition set priority = 2
where 1=1
and id_agenda_has_segment_definition =  (
    select id from agenda_has_segment_definition
    where id_agenda = (select id from agenda where name = 'tournaments')
     and id_agenda_segment = (select id from agenda_segment where name = 'tournament_information')
)
and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_information_main');

--
-- Ředitel soutěže
--

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'telephone')
      , 30, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition
            where id_agenda = (select id from agenda where name = 'tournaments')
            and id_agenda_segment = (select id from agenda_segment where name = 'tournament_information')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_director_main')
          )
);

UPDATE agenda_definition set id_agenda_segment_attribute_group_definition = (
   select id
   from agenda_has_segment_has_attribute_group_definition
   where id_agenda_has_segment_definition = (
        select id from agenda_has_segment_definition
        where id_agenda = (select id from agenda where name = 'tournaments')
        and id_agenda_segment = (select id from agenda_segment where name = 'tournament_information')
   )
   and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_director_main')
)
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournament_information'
    AND attribute_group_name = 'tournament_information_main'
    AND attribute_name in ('agenda_tournament_administrator', 'tournament_contact', 'email')
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

---- #13760 ----
INSERT INTO agenda_button (id_agenda, name, description, icon, class)
VALUES ((SELECT id FROM agenda WHERE name = 'clubs'), 'ClubAres', 'Ares', 'fal fa-down-to-line', 'btn-info');

INSERT INTO agenda_action (name, description, class_name, is_via_rabbit) VALUES ('ClubAresBasic', 'Základní údaje z ARES', 'ClubAresBasicAction', TRUE);
INSERT INTO agenda_action (name, description, class_name, is_via_rabbit) VALUES ('ClubAresOr', 'OR údaje z ARES', 'ClubAresOrAction', TRUE);

INSERT INTO agenda_button_has_action (id_agenda_button, id_agenda_action, priority) VALUES ((SELECT id FROM agenda_button WHERE name = 'ClubAres'), (SELECT id FROM agenda_action WHERE name = 'ClubAresBasic'), 10);
INSERT INTO agenda_button_has_action (id_agenda_button, id_agenda_action, priority) VALUES ((SELECT id FROM agenda_button WHERE name = 'ClubAres'), (SELECT id FROM agenda_action WHERE name = 'ClubAresOr'), 20);


--- #13631 ---
UPDATE agenda_definition set default_value = 0 where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'members_main'
    AND attribute_name in ('suspended', 'honorary_member')
);

UPDATE agenda_definition set default_value = 43 where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'members_address_main'
    AND attribute_name = 'country'
);

UPDATE agenda_definition set default_value = '@@current_date@@', is_read_only = true where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'members_main'
    AND attribute_name = 'dt_of_registration'
);

-- remove values where
DELETE FROM agenda_values WHERE id_agenda_definition IN (
     SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'members'
        AND segment_name = 'members_main'
        AND attribute_group_name = 'members_main'
        AND attribute_name = 'dt_of_change'
);

-- delete agenda definition
DELETE FROM agenda_definition WHERE id IN (
    SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'members'
        AND segment_name = 'members_main'
        AND attribute_group_name = 'members_main'
        AND attribute_name = 'dt_of_change'
);


UPDATE agenda_definition set default_value = 1 where id in (
    SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'members'
        AND segment_name = 'members_main'
        AND attribute_group_name = 'members_main'
        AND attribute_name ='is_active'
);

UPDATE agenda_definition set is_in_datagrid = true where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'members_main'
    AND attribute_name in ('dt_birthdate', 'dt_birthyear', 'clubs_has_members', 'member_fee_current_year')
);

UPDATE agenda_definition set is_in_datagrid = true, is_hidden_in_datagrid = true where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'kyu'
    AND attribute_group_name = 'kyu'
    AND attribute_name = 'kyu_level'
);

UPDATE agenda_definition set is_in_datagrid = true, is_hidden_in_datagrid = true where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'dan'
    AND attribute_group_name = 'dan'
    AND attribute_name = 'dan_level'
);

UPDATE agenda_definition set is_in_datagrid = true, is_hidden_in_datagrid = true where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'honor_regional'
    AND attribute_group_name = 'honor_regional'
    AND attribute_name = 'level_of_honor'
);

UPDATE agenda_definition set is_in_datagrid = true, is_hidden_in_datagrid = true where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'honor_union'
    AND attribute_group_name = 'honor_union'
    AND attribute_name = 'level_of_honor'
);

UPDATE agenda_definition set is_in_datagrid = true, is_hidden_in_datagrid = true where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'coach'
    AND attribute_group_name = 'coach'
    AND attribute_name = 'coach_level'
);

UPDATE agenda_definition set is_in_datagrid = true, is_hidden_in_datagrid = true where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'examiner'
    AND attribute_group_name = 'examiner'
    AND attribute_name = 'examiner_level'
);

UPDATE agenda_definition set is_in_datagrid = true, is_hidden_in_datagrid = true where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'referee'
    AND attribute_group_name = 'referee'
    AND attribute_name = 'referee_level'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

--- #13633 ---

-- remove values where
DELETE FROM agenda_values WHERE id_agenda_definition IN (
     SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'clubs'
        AND segment_name = 'clubs_main'
        AND attribute_group_name = 'clubs_main'
        AND attribute_name = 'dt_of_change'
);

-- delete agenda definition
DELETE FROM agenda_definition WHERE id IN (
    SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'clubs'
        AND segment_name = 'clubs_main'
        AND attribute_group_name = 'clubs_main'
        AND attribute_name = 'dt_of_change'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- #13813 --

update agenda_has_segment_has_attribute_group_definition set priority = 2
where 1=1
and id_agenda_has_segment_definition =  (
    select id from agenda_has_segment_definition
    where id_agenda = (select id from agenda where name = 'tournaments')
     and id_agenda_segment = (select id from agenda_segment where name = 'tournament_information')
)
and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_director_main');

update agenda_has_segment_has_attribute_group_definition set priority = 3
where 1=1
and id_agenda_has_segment_definition =  (
    select id from agenda_has_segment_definition
    where id_agenda = (select id from agenda where name = 'tournaments')
     and id_agenda_segment = (select id from agenda_segment where name = 'tournament_information')
)
and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_information_main');

INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('tournament_administrator_info_main', false, NULL);


--agenda_has_segment_has_attribute_group_definition
INSERT INTO agenda_has_segment_has_attribute_group_definition (id_agenda_has_segment_definition, id_agenda_attribute_group, is_read_only, priority)
VALUES(
          (select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'tournaments')
             and id_agenda_segment = (select id from agenda_segment where name = 'tournament_information')
          )
      , (select id from agenda_attribute_group where name = 'tournament_administrator_info_main')
      , false, 1
      );

-- move agenda_tournament_administrator to tournament_administration group
UPDATE agenda_definition set id_agenda_segment_attribute_group_definition = (
   select id
   from agenda_has_segment_has_attribute_group_definition
   where id_agenda_has_segment_definition = (
        select id from agenda_has_segment_definition
        where id_agenda = (select id from agenda where name = 'tournaments')
        and id_agenda_segment = (select id from agenda_segment where name = 'tournament_information')
   )
   and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_administrator_info_main')
)
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournament_information'
    AND attribute_group_name = 'tournament_director_main'
    AND attribute_name = 'agenda_tournament_administrator'
);

UPDATE agenda_definition set is_in_datagrid = false where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournament_information'
    AND attribute_group_name = 'tournament_director_main'
    AND attribute_name = 'tournament_contact'
);

UPDATE agenda_definition set default_value = null where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournament_information'
    AND attribute_group_name = 'tournament_administrator_info_main'
    AND attribute_name = 'is_active'
);

UPDATE agenda_definition set is_not_null = true where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournament_information'
    AND attribute_group_name = 'tournament_administrator_info_main'
    AND attribute_name = 'agenda_tournament_administrator'
);

UPDATE agenda_definition set is_not_null = true where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournament_information'
    AND attribute_group_name = 'tournament_information_main'
    AND attribute_name = 'is_active'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- #13813  agenda - tournament --

-- Administrátor turnaje 2
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('agenda_tournament_administrator_2', 'Administrátor turnaje 2', 0, 'tournaments_has_tournament_administrator', NULL, 1, (select id from attribute_data_type where name = 'agenda_has_agenda'));


INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'agenda_tournament_administrator_2')
      , 10, false, null, false, false, NULL, 3, 0, 1, false, false, null, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'tournaments')
            and id_agenda_segment = (select id from agenda_segment where name = 'tournament_information')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_administrator_info_main')
          )
      );

-- Administrátor turnaje 3
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('agenda_tournament_administrator_3', 'Administrátor turnaje 3', 0, 'tournaments_has_tournament_administrator', NULL, 1, (select id from attribute_data_type where name = 'agenda_has_agenda'));


INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'agenda_tournament_administrator_3')
      , 15, false, null, false, false, NULL, 3, 0, 1, false, false, null, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'tournaments')
            and id_agenda_segment = (select id from agenda_segment where name = 'tournament_information')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_administrator_info_main')
          )
      );

-- remove all values for definition dt_of_change (tournament)
delete from agenda_values where id_agenda_definition in (
     SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'tournaments'
        AND segment_name = 'tournament_information'
        AND attribute_group_name = 'tournament_information_main'
        AND attribute_name = 'dt_of_change'
);

-- delete agenda definition
delete from agenda_definition where id in (
     SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'tournaments'
        AND segment_name = 'tournament_information'
        AND attribute_group_name = 'tournament_information_main'
        AND attribute_name = 'dt_of_change'
);

UPDATE agenda_definition set default_value = '@@current_date@@' where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournament_information'
    AND attribute_group_name = 'tournament_information_main'
    AND attribute_name = 'dt_of_registration'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- #13813 changes --
UPDATE agenda_definition set max_length = 4000 where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournaments_main'
    AND attribute_group_name = 'tournaments_main'
    AND attribute_name = 'name'
);

UPDATE agenda_definition set default_value = (select id from lookup_tournament_type where name = 'standard')
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournaments_main'
    AND attribute_group_name = 'tournaments_main'
    AND attribute_name = 'tournament_type'
);

UPDATE agenda_definition set is_not_null = false
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournaments_main'
    AND attribute_group_name = 'tournaments_main'
    AND attribute_name in ('dt_term_for_registration', 'web_link_for_registration', 'tournament_starting_fee')
);

UPDATE attribute set lookup = null, id_attribute_data_type = (select id from attribute_data_type where name = 'bool')
where id in (
SELECT id_attribute FROM mv_agenda_definition
    WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournaments_main'
    AND attribute_group_name = 'tournaments_main'
    AND attribute_name in ('international_tournament', 'ranking_list_inclusion')
);

UPDATE agenda_definition set default_value = 0
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournaments_main'
    AND attribute_group_name = 'tournaments_main'
    AND attribute_name in ('international_tournament', 'ranking_list_inclusion')
);

-- UPDATE agenda_definition set is_not_null = false
-- where id in (
-- SELECT id
--      FROM mv_agenda_definition
--      WHERE agenda_name = 'tournaments'
--        AND segment_name = 'tournaments_main'
--        AND attribute_group_name = 'tournaments_address_main'
--        AND attribute_name = 'street'
--  );

UPDATE agenda_definition set default_value = (select id from lookup_country where alpha2 = 'CZ'), is_read_only = true
where id in (
SELECT id
     FROM mv_agenda_definition
     WHERE agenda_name = 'tournaments'
       AND segment_name = 'tournaments_main'
       AND attribute_group_name = 'tournaments_address_main'
       AND attribute_name = 'country'
 );

UPDATE agenda_definition set is_not_null = true
where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournament_information'
    AND attribute_group_name = 'tournament_director_main'
    AND attribute_name in ('email', 'telephone')
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;
REFRESH MATERIALIZED VIEW lookup_judo_categories;

-- #13631 agenda members changes --

UPDATE agenda_definition set is_read_only = false where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'members_main'
    AND attribute_name = 'dt_of_registration'
);

UPDATE agenda_definition set is_read_only = false where id in (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'members_main'
    AND attribute_name = 'dt_of_registration'
);
-- remove values where
DELETE FROM agenda_values WHERE id_agenda_definition IN (
     SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'members'
        AND segment_name = 'members_main'
        AND attribute_group_name = 'members_main'
        AND attribute_name = 'is_active'
);

-- delete agenda definition
DELETE FROM agenda_definition WHERE id IN (
    SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'members'
        AND segment_name = 'members_main'
        AND attribute_group_name = 'members_main'
        AND attribute_name = 'is_active'
);

-- delete show column id member in aha tournaments_has_members --
DELETE FROM agenda_has_agenda_show_attribute
WHERE 1=1
AND id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'tournaments_has_members')
AND id_agenda_definition = (
SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'members_main'
    AND attribute_name = 'id_member'
);

UPDATE agenda_definition set is_read_only = true where id in (
SELECT id FROM mv_agenda_definition
WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournaments_main'
    AND attribute_group_name = 'tournaments_main'
    AND attribute_name = 'tournament_status'
);

-- #13775
UPDATE agenda_definition
SET is_read_only = true
WHERE id = (SELECT id
            FROM mv_agenda_definition
            WHERE agenda_name = 'members' AND segment_name = 'members_main' AND attribute_group_name = 'members_main' AND attribute_name = 'nonage');



REFRESH MATERIALIZED VIEW mv_agenda_definition;


--- #13817 ---
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Svazoví funkcionáři
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-- menu
INSERT INTO agenda  (name, icon, is_importable) VALUES('official', 'fad fa-sitemap', false);

INSERT INTO menu_link (name, presenter, action, parameters) VALUES('official_default', 'Agenda:Agenda', 'default', '{"name":"official"}');
INSERT INTO menu_link (name, presenter, action, parameters) VALUES('official_detail', 'Agenda:Agenda', 'detail', '{"name":"official"}');

INSERT INTO menu_item (id_menu_section, id_agenda, name, icon, id_menu_link, priority, is_default)
VALUES(
          (select id from menu_section where name = 'basic_evidence')
      , (select id from agenda where name = 'official')
      , 'official', 'fad fa-sitemap'
      , (select id from menu_link where name = 'official_default'), 10, false);

INSERT INTO menu_item_has_menu_link (id_menu_item, id_menu_link)
VALUES(
          (select id from menu_item where name = 'official')
      , (select id from menu_link where name = 'official_detail')
      );

INSERT INTO agenda_segment ("name", source_table, file_source_table) VALUES('official_main', 'agenda_values', 'agenda_has_file');

--official_main_main
INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('official_main', false, NULL);

--agenda_has_segment_definition
INSERT INTO agenda_has_segment_definition (id_agenda, id_agenda_segment, priority, is_main)
VALUES(
          (select id from agenda where name = 'official')
      , (select id from agenda_segment where name = 'official_main')
      , 1, true
      );

--agenda_has_segment_has_attribute_group_definition
INSERT INTO agenda_has_segment_has_attribute_group_definition (id_agenda_has_segment_definition, id_agenda_attribute_group, is_read_only, priority)
VALUES(
          (select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'official')
             and id_agenda_segment = (select id from agenda_segment where name = 'official_main')
          )
      , (select id from agenda_attribute_group where name = 'official_main')
      , false, 1
      );


--Člen
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'member')
      , 5, true, NULL, false, false, NULL, 3, 0, 1, true, false, NULL, true, true
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'official')
            and id_agenda_segment = (select id from agenda_segment where name = 'official_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'official_main')
          )
      );

--Uživatel
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'appuser')
      , 10, true, NULL, false, false, NULL, 3, 0, 1, true, true, 1, false, true
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'official')
            and id_agenda_segment = (select id from agenda_segment where name = 'official_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'official_main')
          )
      );

INSERT INTO agenda_segment ("name", source_table, file_source_table) VALUES('official_role_main', 'agenda_values', 'agenda_has_file');

--official_role_main
INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('official_role_main', true, NULL);

--agenda_has_segment_definition
INSERT INTO agenda_has_segment_definition (id_agenda, id_agenda_segment, priority, is_main)
VALUES(
          (select id from agenda where name = 'official')
      , (select id from agenda_segment where name = 'official_role_main')
      , 1, false
      );

--agenda_has_segment_has_attribute_group_definition
INSERT INTO agenda_has_segment_has_attribute_group_definition (id_agenda_has_segment_definition, id_agenda_attribute_group, is_read_only, priority)
VALUES(
          (select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'official')
             and id_agenda_segment = (select id from agenda_segment where name = 'official_role_main')
          )
      , (select id from agenda_attribute_group where name = 'official_role_main')
      , false, 5
      );

--Role svazu
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('official_role', 'Role Svazu', 0, 'lookup_official_role', NULL, 1, (select id from attribute_data_type where name = 'lookup'));

INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_csju', 'Předseda ČSJu', 10);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'deputy_chairman_csju_1', '1. místopředseda ČSJu', 20);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'deputy_chairman_csju_2', '2. místopředseda ČSJu', 30);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'general_secretery', 'Generální sekretář ČSJu', 40);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'commission_for_athletes', 'Komise pro sportovce', 50);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'csju_sports_director', 'Sportovní ředitel ČSJu', 60);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_of_the_commission_for_epresentation', 'Předseda komise pro reprezentaci', 70);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_of_the_college_of_dans', 'Předseda Kolegia DANů', 80);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairwoman_committee_of_referees', 'Předsedkyně komise rozhodčích', 90);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_tmk', 'Předseda TMK', 100);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'trainer_rdj', 'Trenér RDJ', 110);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'trainer_rdd', 'Trenér RDD', 120);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'trainer_rdm', 'Trenér RDM', 130);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'trainer_rdz', 'Trenér RDŽ', 140);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_ksju_lbk', 'Předseda KSJu - Liberecký kraj', 150);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_ksju_hkk', 'Předseda KSJu - Královéhradecký kraj', 160);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_ksju_stc', 'Předseda KSJu - Středočeský kraj', 170);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_ksju_jhc', 'Předseda KSJu - Jihočeský kraj', 180);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_ksju_pak', 'Předseda KSJu - Pardubický kraj', 190);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_ksju_pha', 'Předseda KSJu - Praha', 200);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_ksju_jhm', 'Předseda KSJu - Jihomoravský kraj', 210);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_ksju_ulk', 'Předseda KSJu - Ústecký kraj', 220);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_ksju_kvk', 'Předseda KSJu - Karlovarský kraj', 230);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_ksju_vys', 'Předseda KSJu - kraj Vysočina', 240);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_ksju_plk', 'Předseda KSJu - Plzeňský kraj', 250);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_ksju_zlk', 'Předseda KSJu - Zlínský kraj', 260);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_stk_ksju_lbk', 'Předseda STK KSJu - Liberecký kraj', 270);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_stk_ksju_hkk', 'Předseda STK KSJu - Královéhradecký kraj', 280);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_stk_ksju_stc', 'Předseda STK KSJu - Středočeský kraj', 290);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_stk_ksju_jhc', 'Předseda STK KSJu - Jihočeský kraj', 300);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_stk_ksju_pak', 'Předseda STK KSJu - Pardubický kraj', 310);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_stk_ksju_pha', 'Předseda STK KSJu - Praha', 320);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_stk_ksju_jhm', 'Předseda STK KSJu - Jihomoravský kraj', 330);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_stk_ksju_ulk', 'Předseda STK KSJu - Ústecký kraj', 340);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_stk_ksju_kvk', 'Předseda STK KSJu - Karlovarský kraj', 350);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_stk_ksju_vys', 'Předseda STK KSJu - kraj Vysočina', 360);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_stk_ksju_plk', 'Předseda STK KSJu - Plzeňský kraj', 370);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_stk_ksju_zlk', 'Předseda STK KSJu - Zlínský kraj', 380);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_kr_ksju_lbk', 'Předseda KR KSJu - Liberecký kraj', 390);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_kr_ksju_hkk', 'Předseda KR KSJu - Královéhradecký kraj', 400);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_kr_ksju_stc', 'Předseda KR KSJu - Středočeský kraj', 410);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_kr_ksju_jhc', 'Předseda KR KSJu - Jihočeský kraj', 420);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_kr_ksju_pak', 'Předseda KR KSJu - Pardubický kraj', 430);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_kr_ksju_pha', 'Předseda KR KSJu - Praha', 440);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_kr_ksju_jhm', 'Předseda KR KSJu - Jihomoravský kraj', 450);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_kr_ksju_ulk', 'Předseda KR KSJu - Ústecký kraj', 460);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_kr_ksju_kvk', 'Předseda KR KSJu - Karlovarský kraj', 470);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_kr_ksju_vys', 'Předseda KR KSJu - kraj Vysočina', 480);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_kr_ksju_plk', 'Předseda KR KSJu - Plzeňský kraj', 490);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_kr_ksju_zlk', 'Předseda KR KSJu - Zlínský kraj', 500);

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'official_role')
      , 5, true, NULL, false, false, NULL, 3, 0, 1, false, false, NULL, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'official')
            and id_agenda_segment = (select id from agenda_segment where name = 'official_role_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'official_role_main')
          )
      );

--Platnost od
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('dt_validity_from', 'Platnost od', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'date'));

--Platnost od
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'dt_validity_from')
      , 10, true, NULL, false, false, NULL, 3, 0, 1, false, false, NULL, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'official')
            and id_agenda_segment = (select id from agenda_segment where name = 'official_role_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'official_role_main')
          )
      );

--Platnost do
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('dt_validity_to', 'Platnost do', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'date'));

--Platnost do
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'dt_validity_to')
      , 15, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'official')
            and id_agenda_segment = (select id from agenda_segment where name = 'official_role_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'official_role_main')
          )
      );

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'note')
      , 20, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'official')
            and id_agenda_segment = (select id from agenda_segment where name = 'official_role_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'official_role_main')
          )
      );

REFRESH MATERIALIZED VIEW mv_agenda_definition;

--- #13631 ---
update agenda_definition set is_not_null = true
where id = (
    SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'members'
        AND segment_name = 'members_main'
        AND attribute_group_name = 'members_main'
        AND attribute_name = 'telephone'
);


update agenda_definition set is_not_null = true
where id = (
    SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'members'
        AND segment_name = 'members_main'
        AND attribute_group_name = 'members_main'
        AND attribute_name = 'email'
);

-- #13715 --
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES ('lookup_kyu_level', 'kyu_3_2', '3/2', 75);

UPDATE agenda_definition set default_value = 1
where 1=1
AND id in (
SELECT id FROM mv_agenda_definition
WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournament_information'
    AND attribute_group_name = 'tournament_information_main'
    AND attribute_name = 'is_active'
);

UPDATE agenda_definition set is_not_null = true
where 1=1
AND id in (
SELECT id FROM mv_agenda_definition
WHERE agenda_name = 'tournaments'
    AND segment_name = 'tournaments_main'
    AND attribute_group_name = 'tournaments_address_main'
    AND attribute_name = 'country'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

----------------------------------------------------
-- 2.6.1.0
-- SQL for this version ends here
----------------------------------------------------

-- #13631 --
UPDATE agenda_definition set is_not_null = true
where 1=1
AND id in (
SELECT id FROM mv_agenda_definition
WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'members_contact_main'
    AND attribute_name = 'email'
);

UPDATE agenda_definition set is_not_null = true
where 1=1
AND id in (
SELECT id FROM mv_agenda_definition
WHERE agenda_name = 'members'
    AND segment_name = 'members_main'
    AND attribute_group_name = 'members_contact_main'
    AND attribute_name = 'telephone'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

----------------------------------------------------
-- 2.6.1.1
-- SQL for this version ends here
----------------------------------------------------

-- #13817
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_ksju_olk', 'Předseda KSJu - Olomoucký kraj', 252);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_stk_ksju_olk', 'Předseda STK KSJu - Olomoucký kraj', 372);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_kr_ksju_olk', 'Předseda KR KSJu - Olomoucký kraj', 482);

INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_ksju_msk', 'Předseda KSJu - Moravskoslezský kraj', 254);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_stk_ksju_msk', 'Předseda STK KSJu - Moravskoslezský kraj', 374);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_official_role', 'chairman_kr_ksju_msk', 'Předseda KR KSJu - Moravskoslezský kraj', 484);

-- #13775
INSERT INTO flexii.agenda_definition_specification_by_value (id_agenda_definition, id_agenda_definition_control, value_control, is_hidden, is_not_null)
VALUES ((SELECT id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'members_main' AND attribute_group_name = 'members_main' AND attribute_name = 'members_has_parents2')
       , (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'members_main' AND attribute_group_name = 'members_main' AND attribute_name = 'nonage'), '0', true, false);

INSERT INTO flexii.agenda_definition_specification_by_value (id_agenda_definition, id_agenda_definition_control, value_control, is_hidden, is_not_null)
VALUES ((SELECT id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'members_main' AND attribute_group_name = 'members_main' AND attribute_name = 'members_has_parents')
       , (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'members' AND segment_name = 'members_main' AND attribute_group_name = 'members_main' AND attribute_name = 'nonage'), '0', true, false);


-- #13743
INSERT INTO flexii.agenda_definition_validation (id_agenda_definition, class_name, priority) VALUES
      ((SELECT id FROM mv_agenda_definition WHERE agenda_name = 'clubs' AND segment_name = 'clubs_main' AND attribute_group_name = 'clubs_main' AND attribute_name = 'identifier_idnumber'), 'IcoValidation', 10);

-- agenda icon update --

update menu_item set icon = 'fad fa-family' where name = 'parents';
update menu_item set icon = 'fad fa-user-tie' where name = 'official';

update agenda set icon = 'fad fa-family' where name = 'parents';
update agenda set icon = 'fad fa-user-tie' where name = 'official';

--- #13817 ---
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Trenéři
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-- menu
INSERT INTO agenda  (name, icon, is_importable) VALUES('trainer', 'fad fa-uniform-martial-arts', false);

INSERT INTO menu_link (name, presenter, action, parameters) VALUES('trainer_default', 'Agenda:Agenda', 'default', '{"name":"trainer"}');
INSERT INTO menu_link (name, presenter, action, parameters) VALUES('trainer_detail', 'Agenda:Agenda', 'detail', '{"name":"trainer"}');

INSERT INTO menu_item (id_menu_section, id_agenda, name, icon, id_menu_link, priority, is_default)
VALUES(
        (select id from menu_section where name = 'basic_evidence')
      , (select id from agenda where name = 'trainer')
      , 'trainer', 'fad fa-uniform-martial-arts'
      , (select id from menu_link where name = 'trainer_default'), 20, false);

INSERT INTO menu_item_has_menu_link (id_menu_item, id_menu_link)
VALUES(
          (select id from menu_item where name = 'trainer')
      , (select id from menu_link where name = 'trainer_detail')
      );

INSERT INTO agenda_segment ("name", source_table, file_source_table) VALUES('trainer_main', 'agenda_values', 'agenda_has_file');

--trainer_main
INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('trainer_main', false, NULL);

--agenda_has_segment_definition
INSERT INTO agenda_has_segment_definition (id_agenda, id_agenda_segment, priority, is_main)
VALUES(
    (select id from agenda where name = 'trainer')
    , (select id from agenda_segment where name = 'trainer_main')
    , 1, true
);

--agenda_has_segment_has_attribute_group_definition
INSERT INTO agenda_has_segment_has_attribute_group_definition (id_agenda_has_segment_definition, id_agenda_attribute_group, is_read_only, priority)
VALUES(
    (select id from agenda_has_segment_definition
        where id_agenda = (select id from agenda where name = 'trainer')
         and id_agenda_segment = (select id from agenda_segment where name = 'trainer_main')
    )
    , (select id from agenda_attribute_group where name = 'trainer_main')
    , false, 1
);

--trainer_main
INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('trainer_member_autoinfo', false, NULL);


--agenda_has_segment_has_attribute_group_definition
INSERT INTO agenda_has_segment_has_attribute_group_definition (id_agenda_has_segment_definition, id_agenda_attribute_group, is_read_only, priority)
VALUES(
    (select id from agenda_has_segment_definition
        where id_agenda = (select id from agenda where name = 'trainer')
         and id_agenda_segment = (select id from agenda_segment where name = 'trainer_main')
    )
    , (select id from agenda_attribute_group where name = 'trainer_member_autoinfo')
    , false, 2
);

-- AHA Members has trainer
INSERT INTO agenda_has_agenda (name, id_agenda, id_agenda_linked, relation, is_unique, priority, is_quick_add_link, is_quick_add_link_linked, is_quick_add_agenda, is_quick_add_agenda_linked, is_custom_name)
VALUES (
       'members_has_trainer'
       , (select id from agenda where name = 'members')
       , (select id from agenda where name = 'trainer')
       , 3, true, 200, false, false, false, false, true);

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('members_has_trainer', 'Člen', 0, 'members_has_trainer', NULL, 1, (select id from attribute_data_type where name = 'agenda_has_agenda'));

--Členové
INSERT INTO agenda_has_agenda_definition (id_agenda_has_agenda, id_attribute, priority, is_not_null, is_unique, default_value) VALUES(
(select id from agenda_has_agenda where name = 'members_has_trainer')
, (select id from attribute where name = 'members_has_trainer'), 10, true, true, null);

--Člen
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'members_has_trainer')
      , 5, true, NULL, false, false, NULL, 3, 0, 1, false, false, NULL, true, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'trainer')
            and id_agenda_segment = (select id from agenda_segment where name = 'trainer_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'trainer_main')
          )
      );

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('id_member_sync', 'ID Člena', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'int'));

-- ID člena
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'id_member_sync')
      , 10, false, NULL, false, true, NULL, 3, 0, 1, true, true, 1, true, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'trainer')
            and id_agenda_segment = (select id from agenda_segment where name = 'trainer_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'trainer_member_autoinfo')
          )
      );

-- Jméno člena
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'firstname')
      , 15, false, NULL, false, true, NULL, 3, 0, 1, true, true, 2, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'trainer')
            and id_agenda_segment = (select id from agenda_segment where name = 'trainer_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'trainer_member_autoinfo')
          )
      );

-- Příjmení člena
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'surname')
      , 20, false, NULL, false, true, NULL, 3, 0, 1, true, true, 3, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'trainer')
            and id_agenda_segment = (select id from agenda_segment where name = 'trainer_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'trainer_member_autoinfo')
          )
      );

-- Záložka - Trenérská třída
INSERT INTO agenda_segment ("name", source_table, file_source_table) VALUES('trainer_class_main', 'agenda_values', 'agenda_has_file');

--trainer_class_main
INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('trainer_class_main', true, NULL);

--agenda_has_segment_definition
INSERT INTO agenda_has_segment_definition (id_agenda, id_agenda_segment, priority, is_main)
VALUES(
        (select id from agenda where name = 'trainer')
      , (select id from agenda_segment where name = 'trainer_class_main')
      , 5, false
      );

--agenda_has_segment_has_attribute_group_definition
INSERT INTO agenda_has_segment_has_attribute_group_definition (id_agenda_has_segment_definition, id_agenda_attribute_group, is_read_only, priority)
VALUES(
          (select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'trainer')
             and id_agenda_segment = (select id from agenda_segment where name = 'trainer_class_main')
          )
      , (select id from agenda_attribute_group where name = 'trainer_class_main')
      , false, 5
      );

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('trainer_class', 'Trenérská třída', 0, 'lookup_trainer_class', NULL, 1, (select id from attribute_data_type where name = 'lookup'));

INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_trainer_class', 'coach_trainer', 'Trenér cvičitel', 10);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_trainer_class', 'third_tc', 'III. třída', 20);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_trainer_class', 'second_tc', 'II. třída', 30);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_trainer_class', 'first_tc', 'I. třída', 40);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_trainer_class', 'certified_trainer', 'Diplomovaný trenér', 50);

INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'trainer_class')
      , 5, true, NULL, false, false, NULL, 3, 0, 1, false, false, NULL, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'trainer')
            and id_agenda_segment = (select id from agenda_segment where name = 'trainer_class_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'trainer_class_main')
          )
      );

--Platnost od
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'dt_validity_from')
      , 10, true, NULL, false, false, NULL, 3, 0, 1, false, false, NULL, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'trainer')
            and id_agenda_segment = (select id from agenda_segment where name = 'trainer_class_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'trainer_class_main')
          )
      );

--Platnost do
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'dt_validity_to')
      , 15, false, NULL, false, false, NULL, 3, 0, 1, false, false, NULL, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'trainer')
            and id_agenda_segment = (select id from agenda_segment where name = 'trainer_class_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'trainer_class_main')
          )
      );

-- -- Záložka - Kreditn body
INSERT INTO agenda_segment ("name", source_table, file_source_table) VALUES('credit_point_main', 'agenda_values', 'agenda_has_file');

--trainer_class_main
INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('credit_point_main', true, NULL);

--agenda_has_segment_definition
INSERT INTO agenda_has_segment_definition (id_agenda, id_agenda_segment, priority, is_main)
VALUES(
        (select id from agenda where name = 'trainer')
      , (select id from agenda_segment where name = 'credit_point_main')
      , 10, false
      );

--agenda_has_segment_has_attribute_group_definition
INSERT INTO agenda_has_segment_has_attribute_group_definition (id_agenda_has_segment_definition, id_agenda_attribute_group, is_read_only, priority)
VALUES(
          (select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'trainer')
             and id_agenda_segment = (select id from agenda_segment where name = 'credit_point_main')
          )
      , (select id from agenda_attribute_group where name = 'credit_point_main')
      , false, 10
      );

-- číselník kreditních bodů
