INSERT INTO user_group_has_agenda_definition (id_user_group, id_agenda_definition, is_read, is_write)
SELECT usr.id, ad.id, TRUE, FALSE
FROM (SELECT id
      FROM user_group
      WHERE name IN ('Clenove_ClenoveKlubuAdmina_EditLimitovan+Pridani')
      ) usr,
     mv_agenda_definition ad
WHERE ad.agenda_name = 'members' AND (ad.segment_name, ad.attribute_group_name, ad.attribute_name) IN (
    ('members_main', 'members_main', 'dt_of_registration')
);

REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_definition;
REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_permission;

----------------------------------------------------
-- 2.6.10.5
-- SQL for this version ends here
----------------------------------------------------

-- remove trash
DELETE
FROM agenda_has_agenda_values
WHERE id_agenda_has_agenda_definition = (SELECT id
                                         FROM agenda_has_agenda_definition
                                         WHERE id_attribute = (SELECT id FROM attribute WHERE name = 'clubs_officials_has_members'));

DELETE
FROM agenda_has_agenda_definition
WHERE id_attribute = (SELECT id FROM attribute WHERE name = 'clubs_officials_has_members');

DELETE FROM attribute WHERE name = 'clubs_officials_has_members';
DELETE FROM attribute WHERE name = 'agenda_tournament_administrator_2';
DELETE FROM attribute WHERE name = 'agenda_tournament_administrator_3';
DELETE FROM attribute WHERE name = 'tournaments_has_members';

-- #14651
UPDATE user_group_has_agenda_has_agenda SET is_can_write = true, is_can_create = true, is_can_delete = true
WHERE id_user_group = (SELECT id FROM base.user_group WHERE name = 'Kluby_AdminemKlubu_EditLimitovan+ObjednaniPoplatku+TiskKaret')
AND id_agenda_has_agenda= (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_officials_has_members');

REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_has_agenda;

INSERT INTO flexii.definition_table_value (id_definition_key, id_definition, value) VALUES
   ((SELECT id FROM definition_key WHERE name = 'is_datagrid_filter_key_as_lookup_name' AND table_name = '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 = (SELECT id FROM attribute WHERE name = 'weight_category')), true);

INSERT INTO flexii.definition_table_value (id_definition_key, id_definition, value) VALUES
   ((SELECT id FROM definition_key WHERE name = 'is_datagrid_filter_key_as_lookup_name' AND table_name = '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 = (SELECT id FROM attribute WHERE name = 'age_category')), true);

REFRESH MATERIALIZED VIEW mv_agenda_has_agenda_definition_property;

-- #14426
UPDATE flexii.lookup_weightcategories SET name = 'all_u8' WHERE name = 'all' AND description = 'U8';
UPDATE flexii.lookup_weightcategories SET name = 'all_u10' WHERE name = 'all' AND description = 'U10';
REFRESH MATERIALIZED VIEW lookup_judo_categories;

-- #14425
INSERT INTO agenda_definition_validation_by_operator (id_agenda_definition, operator, id_agenda_definition_second)
VALUES ((SELECT id
         from mv_agenda_definition
         WHERE agenda_name = 'tournaments'
           AND segment_name = 'tournaments_main'
           AND attribute_group_name = 'tournaments_main'
           AND attribute_name = 'dt_tournament_start'),
        '<',
        (SELECT id
         from mv_agenda_definition
         WHERE agenda_name = 'tournaments'
           AND segment_name = 'tournaments_main'
           AND attribute_group_name = 'tournaments_main'
           AND attribute_name = 'dt_tournament_end')
        );

----------------------------------------------------
-- 2.6.11.0
-- SQL for this version ends here
----------------------------------------------------

-- #13986
INSERT INTO user_group (name, is_admin_group, is_superadmin_group, description, is_system_group) VALUES ('ext_k_solution', false, false, 'Práva externí aplikace K-Solution', true);
INSERT INTO user_group (name, is_admin_group, is_superadmin_group, description, is_system_group) VALUES ('ext_mups', false, false, 'Práva externí aplikace MUPS', true);
INSERT INTO user_group (name, is_admin_group, is_superadmin_group, description, is_system_group) VALUES ('ext_esports', false, false, 'Práva externí aplikace E-Sports', true);

INSERT INTO user_group_has_agenda (id_user_group, id_agenda, is_can_read, is_can_write, is_can_create, is_can_delete)
values ((select id from base.user_group where name = 'ext_k_solution'), (select id from agenda where name = 'clubs'), true, false, false, false);

INSERT INTO user_group_has_agenda (id_user_group, id_agenda, is_can_read, is_can_write, is_can_create, is_can_delete)
values ((select id from base.user_group where name = 'ext_mups'), (select id from agenda where name = 'referee'), true, false, false, false);

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

INSERT INTO user_group_has_appuser (id_user_group, id_appuser) VALUES ((select id from base.user_group where name = 'ext_esports'), (select id from base.appuser where username = 'ext_esports'));
INSERT INTO user_group_has_appuser (id_user_group, id_appuser) VALUES ((select id from base.user_group where name = 'ext_mups'), (select id from base.appuser where username = 'ext_mups'));
INSERT INTO user_group_has_appuser (id_user_group, id_appuser) VALUES ((select id from base.user_group where name = 'ext_k_solution'), (select id from base.appuser where username = 'ext_k_solution'));

-- ext_esports
INSERT INTO user_group_has_agenda_has_segment_definition (id_user_group, id_agenda_has_segment_definition, is_read, is_write)
VALUES ((SELECT id FROM base.user_group WHERE name = 'ext_esports'), (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')),
        FALSE, FALSE);

INSERT INTO user_group_has_agenda_has_segment_group_definition (id_user_group, id_agenda_has_segment_has_attribute_group_definition, is_read, is_write)
VALUES ((SELECT id FROM base.user_group WHERE name = 'ext_esports'), (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')),
        FALSE, FALSE);

INSERT INTO user_group_has_agenda_has_agenda (id_user_group, id_agenda_has_agenda, is_can_read, is_can_write, is_can_create, is_can_delete)
VALUES ((SELECT id FROM base.user_group WHERE name = 'ext_esports'), (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_members'), FALSE,
        FALSE, FALSE, FALSE);

INSERT INTO user_group_has_agenda_has_agenda (id_user_group, id_agenda_has_agenda, is_can_read, is_can_write, is_can_create, is_can_delete)
VALUES ((SELECT id FROM base.user_group WHERE name = 'ext_esports'), (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_competitions'), FALSE,
        FALSE, FALSE, FALSE);

-- ext_k_solution
INSERT INTO user_group_has_agenda_has_agenda (id_user_group, id_agenda_has_agenda, is_can_read, is_can_write, is_can_create, is_can_delete)
VALUES ((SELECT id FROM base.user_group WHERE name = 'ext_k_solution'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), FALSE,
        FALSE, FALSE, FALSE);

-- parent unique definitions group
INSERT INTO agenda_definition_unique (id_agenda, name, description, is_agenda_unique, is_entity_unique)
VALUES ((SELECT id FROM agenda WHERE name = 'parents'),
        'parents_fullname_telephone',
        'Zákonný zástupce a jeho Příjmení, Jméno a Telefon',
        TRUE,
        FALSE);

INSERT INTO agenda_definition_unique_definition (id_agenda_definition_unique, id_agenda_definition) VALUES ((SELECT id FROM agenda_definition_unique WHERE name = 'parents_fullname_telephone'), (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'parents-parents_main-parents_main-firstname'));
INSERT INTO agenda_definition_unique_definition (id_agenda_definition_unique, id_agenda_definition) VALUES ((SELECT id FROM agenda_definition_unique WHERE name = 'parents_fullname_telephone'), (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'parents-parents_main-parents_main-surname'));
INSERT INTO agenda_definition_unique_definition (id_agenda_definition_unique, id_agenda_definition) VALUES ((SELECT id FROM agenda_definition_unique WHERE name = 'parents_fullname_telephone'), (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'parents-parents_main-parents_contact_main-telephone'));

INSERT INTO agenda_definition_unique (id_agenda, name, description, is_agenda_unique, is_entity_unique)
VALUES ((SELECT id FROM agenda WHERE name = 'parents'),
        'parents_fullname_email',
        'Zákonný zástupce a jeho Příjmení, Jméno a E-mail',
        TRUE,
        FALSE);

INSERT INTO agenda_definition_unique_definition (id_agenda_definition_unique, id_agenda_definition) VALUES ((SELECT id FROM agenda_definition_unique WHERE name = 'parents_fullname_email'), (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'parents-parents_main-parents_main-firstname'));
INSERT INTO agenda_definition_unique_definition (id_agenda_definition_unique, id_agenda_definition) VALUES ((SELECT id FROM agenda_definition_unique WHERE name = 'parents_fullname_email'), (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'parents-parents_main-parents_main-surname'));
INSERT INTO agenda_definition_unique_definition (id_agenda_definition_unique, id_agenda_definition) VALUES ((SELECT id FROM agenda_definition_unique WHERE name = 'parents_fullname_email'), (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'parents-parents_main-parents_contact_main-email'));

-- 14669
DELETE
FROM agenda_has_agenda_definition
WHERE id_agenda_has_agenda =
      (select id from agenda_has_agenda where name = 'members_has_referee')
  AND id_attribute = (select id from attribute where name = 'members_has_referee');

DELETE
FROM agenda_has_agenda_definition
WHERE id_agenda_has_agenda =
      (select id from agenda_has_agenda where name = 'members_has_club_administrator')
  AND id_attribute = (select id from attribute where name = 'members_has_club_administrator');

DELETE
FROM agenda_has_agenda_definition
WHERE id_agenda_has_agenda =
      (select id from agenda_has_agenda where name = 'members_has_trainer')
  AND id_attribute = (select id from attribute where name = 'members_has_trainer');

DELETE
FROM agenda_has_agenda_definition
WHERE id_agenda_has_agenda =
      (select id from agenda_has_agenda where name = 'members_has_official')
  AND id_attribute = (select id from attribute where name = 'members_has_official');

DELETE
FROM agenda_has_agenda_definition
WHERE id_agenda_has_agenda =
      (select id from agenda_has_agenda where name = 'members_has_tournament_administrator')
  AND id_attribute = (select id from attribute where name = 'members_has_tournament_administrator');

DELETE
FROM agenda_has_agenda_definition
WHERE id_agenda_has_agenda =
      (select id from agenda_has_agenda where name = 'members_has_examiner')
  AND id_attribute = (select id from attribute where name = 'members_has_examiner');

DELETE
FROM agenda_has_agenda_definition
WHERE id_agenda_has_agenda =
      (select id from agenda_has_agenda where name = 'members_has_club_administrator')
  AND id_attribute = (select id from attribute where name = 'members_has_club_administrator');

-- #14515
INSERT INTO flexii.api_agenda_page_attribute (name) VALUES ('member_fee_current_year');
INSERT INTO flexii.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 = 'member_fee_current_year')
       );

INSERT INTO flexii.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 full_unique_name = 'members-members_main-members_main-member_fee_current_year'));

INSERT INTO flexii.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 = 'member_fee_current_year')
                       ),
        (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 full_unique_name = 'members-members_main-members_main-member_fee_current_year')));

refresh materialized view mv_api_agenda_mapping;

----------------------------------------------------
-- 2.6.12.0
-- SQL for this version ends here
----------------------------------------------------

update agenda_definition SET in_title_and_priority = NULL WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'union_fees-union_fees_main-union_fees_main-clubs_has_union_fees');
refresh materialized view mv_agenda_definition;

-- #14368
UPDATE flexii.user_group_has_agenda_has_agenda SET is_can_read = true, is_can_write = true, is_can_create = false, is_can_delete = true WHERE id_user_group = (SELECT id FROM user_group WHERE name = 'Turnaje_TurnajAktivniAPoradanKlubemAdmina_Cteni+PrihlaseniClenuSvehoKlubu') AND id_agenda_has_agenda = (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_members');
UPDATE flexii.user_group_has_agenda_has_agenda SET is_can_read = true, is_can_write = true, is_can_create = false, is_can_delete = true WHERE id_user_group = (SELECT id FROM user_group WHERE name = 'Turnaje_AdminemTurnaje_Edit+Spusteni+TiskHesel') AND id_agenda_has_agenda = (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_members');
refresh materialized view mv_user_group_has_agenda_has_agenda;

-- #14368 - additional fix
update agenda_has_agenda set is_deletable = false where name = 'tournaments_has_members';

----------------------------------------------------
-- 2.6.13.0
-- SQL for this version ends here
----------------------------------------------------

-- #14333
INSERT INTO user_group_has_agenda (id_user_group, id_agenda, is_can_read, is_can_write, is_can_create, is_can_delete)
values ((select id from base.user_group where name = 'ext_mups'), (select id from agenda where name = 'tournaments'), true, false, false, false);

INSERT INTO user_group_has_agenda_has_segment_definition (id_user_group, id_agenda_has_segment_definition, is_read, is_write)
VALUES ((SELECT id FROM base.user_group WHERE name = 'ext_mups'), (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')),
        FALSE, FALSE);

INSERT INTO user_group_has_agenda_has_segment_group_definition (id_user_group, id_agenda_has_segment_has_attribute_group_definition, is_read, is_write)
VALUES ((SELECT id FROM base.user_group WHERE name = 'ext_mups'), (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')),
        FALSE, FALSE);

INSERT INTO user_group_has_agenda_has_agenda (id_user_group, id_agenda_has_agenda, is_can_read, is_can_write, is_can_create, is_can_delete)
VALUES ((SELECT id FROM base.user_group WHERE name = 'ext_mups'), (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_members'), FALSE,
        FALSE, FALSE, FALSE);

INSERT INTO user_group_has_agenda_has_agenda (id_user_group, id_agenda_has_agenda, is_can_read, is_can_write, is_can_create, is_can_delete)
VALUES ((SELECT id FROM base.user_group WHERE name = 'ext_mups'), (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_competitions'), FALSE,
        FALSE, FALSE, FALSE);

----------------------------------------------------
-- 2.6.14.0
-- SQL for this version ends here
----------------------------------------------------

-- #14874
MERGE INTO agenda_values d
USING (SELECT id_year_value, dt_birthdate, new_year, current_dt_birthyear
       FROM (SELECT av2.id AS id_year_value, av.value AS dt_birthdate, RIGHT(av.value, 4) AS new_year, av2.value AS current_dt_birthyear
             FROM agenda_values av
                      JOIN (SELECT id, id_agenda_entity, value
                            FROM agenda_values
                            WHERE id_agenda_definition = (SELECT id
                                                          FROM mv_agenda_definition
                                                          WHERE full_unique_name = 'members-members_main-members_main-dt_birthyear')) av2
                           ON av.id_agenda_entity = av2.id_agenda_entity
             WHERE av.id_agenda_definition =
                   (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-dt_birthdate')
               AND RIGHT(av.value, 4) != av2.value) base) s
ON (d.id = s.id_year_value)
WHEN MATCHED THEN
    UPDATE
    SET value = s.new_year;

----------------------------------------------------
-- 2.6.14.1
-- SQL for this version ends here
----------------------------------------------------

-- #14391
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES ('age_category_text', 'Věková kategorie', 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, is_hidden)
VALUES(
        (select id from attribute where name = 'age_category_text')
      , 148, false, NULL, true, 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 = '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')
        ), true
      );

REFRESH MATERIALIZED VIEW mv_agenda_definition;
REFRESH MATERIALIZED VIEW mv_agenda_definition_property;

ALTER TABLE lookup_categories ADD COLUMN is_official bool DEFAULT true NOT NULL;

UPDATE lookup_categories SET is_official = false WHERE name IN ('2024_mu23', '2024_wu23');

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 = 'clubs_has_members')
       , (select id from mv_agenda_definition where full_unique_name = 'members-members_main-members_main-age_category_text')
       , false);

----------------------------------------------------
-- 2.6.15.0
-- SQL for this version ends here
----------------------------------------------------

--#14495
--rename 'Název - zkrácený' to 'Název ČSJU'
--also change internal name to 'club_csju_name'
UPDATE attribute
    SET description='Název ČSJU',
        name='club_csju_name'
    WHERE name='short_name';
UPDATE api_agenda_page_attribute
    SET name='club_csju_name'
    WHERE name='short_name';
--make required
UPDATE agenda_definition
    SET is_not_null=true
    WHERE id_attribute= (
        SELECT id FROM attribute WHERE name='club_csju_name'
    );

refresh materialized view mv_agenda_definition;

-- --forbid a bunch of stuff for Kluby_AdminemKlubu_EditLimitovan+ObjednaniPoplatku+TiskKaret
INSERT INTO user_group_has_agenda_definition (id_user_group, id_agenda_definition, is_read, is_write)
SELECT usr.id, ad.id, TRUE, FALSE
FROM (SELECT id
      FROM base.user_group
      WHERE name IN
            (
                'Kluby_AdminemKlubu_EditLimitovan+ObjednaniPoplatku+TiskKaret'
                )) usr,
     mv_agenda_definition ad
WHERE ad.agenda_name = 'clubs' AND ad.segment_name = 'clubs_main' AND (ad.attribute_group_name, ad.attribute_name) IN (
       ('clubs_main', 'club_csju_name'),
       ('clubs_main', 'id_club'),
       ('clubs_main', 'club_name'),
       ('clubs_main', 'foreign_club'),
       ('clubs_main', 'club_hajime_acronym'),
       ('clubs_main', 'dt_of_registration')
    )
ON CONFLICT(id_user_group, id_agenda_definition) DO UPDATE
    SET is_read = TRUE,
        is_write = FALSE
;
--
-- --forbit edit CSJU name for Kluby_Vse_Edit+Pridani+TiskKaret
INSERT INTO user_group_has_agenda_definition (id_user_group, id_agenda_definition, is_read, is_write)
SELECT usr.id, ad.id, TRUE, FALSE
FROM (SELECT id
      FROM base.user_group
      WHERE name IN
            ('Kluby_Vse_Edit+Pridani+TiskKaret')
      ) usr,
     mv_agenda_definition ad
WHERE ad.agenda_name = 'clubs' AND ad.segment_name = 'clubs_main' AND (ad.attribute_group_name, ad.attribute_name) IN (
       ('clubs_main', 'club_csju_name')
    )
ON CONFLICT(id_user_group, id_agenda_definition) DO UPDATE
    SET is_read = TRUE,
        is_write = FALSE;


-- #14214
UPDATE agenda_has_agenda SET is_addable = FALSE, is_addable_linked = false WHERE name = 'clubs_has_members_hosting';
UPDATE agenda_has_agenda_definition
SET is_not_null = TRUE
WHERE id_agenda_has_agenda = (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members_hosting')
  AND id_attribute IN (SELECT id FROM attribute WHERE NAME IN ('dt_from', 'dt_to'));


----------------------------------------------------
-- 2.6.15.0
-- SQL for this version ends here
----------------------------------------------------

-- #14900
INSERT INTO user_group_has_agenda (id_user_group, id_agenda, is_can_read, is_can_write, is_can_create, is_can_delete) values ((SELECT id FROM base.user_group WHERE name = 'Clenove_Vse_PridaniDan'), (SELECT id FROM agenda WHERE name = 'official'), true, false, false, false);

INSERT INTO user_group_has_definition_value_for_aha (name, id_recursion, id_user_group, id_agenda, id_agenda_has_agenda, id_agenda_definition, value)
VALUES
    ('members_dan_has_official_lvl_1', null, (SELECT id FROM base.user_group WHERE name = 'Clenove_Vse_PridaniDan'), (SELECT id FROM agenda WHERE name = 'official'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_official'), (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'official-official_main-official_main-appuser') , null)
;

refresh materialized view mv_user_group_has_agenda_definition;
refresh materialized view mv_user_group_has_agenda_permission;

----------------------------------------------------
-- 2.6.15.1
-- SQL for this version ends here
----------------------------------------------------

-----------------------
-- #14365 - 2)
UPDATE agenda_definition
SET in_title_and_priority = null
WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-dt_birthyear');

UPDATE agenda_definition
SET in_title_and_priority = null
WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-id_member');

UPDATE agenda_definition
SET in_title_and_priority = 10,
    priority              = 20
WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-surname');

UPDATE agenda_definition
SET in_title_and_priority = 20,
    priority              = 30
WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-firstname');



UPDATE agenda_definition
SET is_hidden_in_datagrid = true
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('members-members_main-members_main-foreigner',
                                                                            'members-members_main-members_main-gender',
                                                                            'members-members_main-members_main-dt_birthdate',
                                                                            'members-members_main-members_main-dt_of_registration',
                                                                            'members-members_main-members_main-honorary_member')
);

UPDATE agenda_definition
SET datagrid_order_priority = 10, datagrid_order_by = 'ASC'
WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-surname');

UPDATE agenda_definition
SET datagrid_order_priority = 20, datagrid_order_by = 'ASC'
WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-firstname');

-- #14365 - 3)
UPDATE agenda_definition
SET in_title_and_priority = NULL
WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'clubs-clubs_main-clubs_main-id_club');

UPDATE agenda_definition
SET is_hidden_in_datagrid = true
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('clubs-clubs_main-clubs_main-id_club',
                                                                            'clubs-clubs_main-clubs_main-foreign_club',
                                                                            'clubs-clubs_main-clubs_main-taxnumber',
                                                                            'clubs-clubs_main-clubs_main-locations'));

UPDATE agenda_definition
SET is_hidden_in_datagrid = false
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('clubs-clubs_address-clubs_address_main-address_region',
                                                                            'clubs-clubs_address-clubs_address_main-city'));

UPDATE agenda_definition
SET datagrid_order_priority = 10, datagrid_order_by = 'ASC'
WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'clubs-clubs_main-clubs_main-club_csju_name');

UPDATE flexii.agenda_has_agenda SET hide_entity_in_datagrid = true WHERE name = 'clubs_has_members';

INSERT INTO agenda_has_agenda_show_attribute (id_agenda_has_agenda, id_agenda_definition, is_hidden_in_datagrid, is_only_for_export, datagrid_order_priority, datagrid_order_by)
VALUES ((SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-surname'), FALSE, false, 1, 'ASC');

INSERT INTO agenda_has_agenda_show_attribute (id_agenda_has_agenda, id_agenda_definition, is_hidden_in_datagrid, is_only_for_export, datagrid_order_priority, datagrid_order_by)
VALUES ((SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-firstname'), FALSE, false, 2, 'ASC');

INSERT INTO agenda_has_agenda_show_attribute (id_agenda_has_agenda, id_agenda_definition, is_hidden_in_datagrid, is_only_for_export, datagrid_order_priority, datagrid_order_by)
VALUES ((SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-id_member'), FALSE, false, NULL, NULL);

-- #14365 - 4)
UPDATE agenda_definition
SET in_title_and_priority = 20, priority = 30, datagrid_order_priority = 2, datagrid_order_by = 'ASC'
WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'parents-parents_main-parents_main-firstname');

UPDATE agenda_definition
SET in_title_and_priority = 10, priority = 20, datagrid_order_priority = 1, datagrid_order_by = 'ASC'
WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'parents-parents_main-parents_main-surname');

UPDATE agenda_definition
SET is_hidden_in_datagrid = true
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN (
                                                                            'parents-parents_main-parents_main-id_parent',
                                                                            'parents-parents_main-parents_main-dt_of_registration',
                                                                            'parents-parents_main-parents_contact_main-email',
                                                                            'parents-parents_main-parents_contact_main-telephone'
    ));

UPDATE agenda_definition
SET is_in_datagrid = true
WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'parents-parents_main-parents_address_main-city');

-- #14365 - 5)
UPDATE agenda_definition
SET is_hidden_in_datagrid = true
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN (
    'club_administrator-club_administrator_main-club_administrator_main-members_has_club_administrator'
    ));

-- #14365 - 6)
UPDATE agenda_definition
SET in_title_and_priority = 30, priority = 30, datagrid_order_priority = 2, datagrid_order_by = 'ASC'
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('official-official_main-official_member_autoinfo-firstname'));

UPDATE agenda_definition
SET in_title_and_priority = 20, priority = 20, datagrid_order_priority = 1, datagrid_order_by = 'ASC'
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('official-official_main-official_member_autoinfo-surname'));

-- #14365 - 7)
-- Rok narození
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_importable, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'dt_birthyear')
      , 40, false, NULL, true, false, NULL, 3, 0, 1, true, false, NULL, 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 = 'referee')
                                                                                                  and id_agenda_segment = (select id from agenda_segment where name = 'referee_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'referee_member_autoinfo')
        )
      );

REFRESH MATERIALIZED VIEW mv_agenda_definition;

INSERT INTO agenda_definition_to_agenda_definition_mapping (name, id_agenda_definition, id_agenda_definition_linked)
VALUES (
           'member_referee-dt_birthyear_dt_birthyear',
           (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-dt_birthyear'),
           (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'referee-referee_main-referee_member_autoinfo-dt_birthyear')
       );

INSERT INTO flexii.sync_has_mapping (id_agenda_to_agenda_sync, id_agenda_definition_to_agenda_definition_mapping)
VALUES ((SELECT id FROM agenda_to_agenda_sync WHERE name = 'members_has_referee_agenda_sync'), (SELECT id FROM agenda_definition_to_agenda_definition_mapping WHERE name = 'member_referee-dt_birthyear_dt_birthyear'));


UPDATE agenda_definition
SET priority = 19, datagrid_order_priority = 2, datagrid_order_by = 'ASC'
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('referee-referee_main-referee_member_autoinfo-firstname'));

UPDATE agenda_definition
SET priority = 15, datagrid_order_priority = 1, datagrid_order_by = 'ASC'
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('referee-referee_main-referee_member_autoinfo-surname'));



-- #14365 - 8)
-- Rok narození
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_importable, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'dt_birthyear')
      , 33, false, NULL, true, false, NULL, 3, 0, 1, true, false, NULL, 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 = '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')
        )
      );

REFRESH MATERIALIZED VIEW mv_agenda_definition;

INSERT INTO agenda_definition_to_agenda_definition_mapping (name, id_agenda_definition, id_agenda_definition_linked)
VALUES (
        'member_trainer-dt_birthyear_dt_birthyear',
        (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-dt_birthyear'),
        (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'trainer-trainer_main-trainer_member_autoinfo-dt_birthyear')
       );

INSERT INTO flexii.sync_has_mapping (id_agenda_to_agenda_sync, id_agenda_definition_to_agenda_definition_mapping)
VALUES ((SELECT id FROM agenda_to_agenda_sync WHERE name = 'members_has_trainer_agenda_sync'), (SELECT id FROM agenda_definition_to_agenda_definition_mapping WHERE name = 'member_trainer-dt_birthyear_dt_birthyear'));

UPDATE agenda_definition
SET priority = 19, datagrid_order_priority = 2, datagrid_order_by = 'ASC'
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('trainer-trainer_main-trainer_member_autoinfo-firstname'));

UPDATE agenda_definition
SET priority = 15, datagrid_order_priority = 1, datagrid_order_by = 'ASC'
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('trainer-trainer_main-trainer_member_autoinfo-surname'));


-- #14365 - 9)
-- Rok narození
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_importable, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'dt_birthyear')
      , 33, false, NULL, true, false, NULL, 3, 0, 1, true, false, NULL, 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 = 'examiner')
                                                                                                  and id_agenda_segment = (select id from agenda_segment where name = 'examiner_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'examiner_member_autoinfo')
        )
      );

REFRESH MATERIALIZED VIEW mv_agenda_definition;

INSERT INTO agenda_definition_to_agenda_definition_mapping (name, id_agenda_definition, id_agenda_definition_linked)
VALUES (
           'member_examiner-dt_birthyear_dt_birthyear',
           (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-dt_birthyear'),
           (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'examiner-examiner_main-examiner_member_autoinfo-dt_birthyear')
       );

INSERT INTO flexii.sync_has_mapping (id_agenda_to_agenda_sync, id_agenda_definition_to_agenda_definition_mapping)
VALUES ((SELECT id FROM agenda_to_agenda_sync WHERE name = 'members_has_examiner_agenda_sync'), (SELECT id FROM agenda_definition_to_agenda_definition_mapping WHERE name = 'member_examiner-dt_birthyear_dt_birthyear'));


UPDATE agenda_definition
SET priority = 19, datagrid_order_priority = 2, datagrid_order_by = 'ASC'
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('examiner-examiner_main-examiner_member_autoinfo-firstname'));

UPDATE agenda_definition
SET priority = 15, datagrid_order_priority = 1, datagrid_order_by = 'ASC'
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('examiner-examiner_main-examiner_member_autoinfo-surname'));

-- #14365 - 10)
UPDATE agenda_definition
SET datagrid_order_priority = 1, datagrid_order_by = 'DESC'
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('union_fees-union_fees_main-union_fees_main-dt_of_payment_issue'));

-- #14365 - 11)
UPDATE agenda_definition
SET is_hidden_in_datagrid = true
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('tournaments-tournaments_main-tournaments_main-id_tournament'));

UPDATE agenda_definition
SET datagrid_order_priority = 1, datagrid_order_by = 'DESC'
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('tournaments-tournaments_main-tournaments_main-dt_tournament_start'));

INSERT INTO agenda_datagrid_default_filter (id_agenda_definition, value)
VALUES (
        (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'tournaments-tournaments_main-tournaments_main-tournament_status'),
        (SELECT id FROM master_lookup WHERE lookup_name = 'lookup_tournament_status' AND name = 'planned')
);

-- #14365 - 12)
UPDATE agenda_definition
SET priority = 2, in_title_and_priority = null, is_hidden_in_datagrid = true
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('tournament_administrator-tournament_administrator_main-tournament_administrator_member_autoinfo-id_member_sync'));

UPDATE agenda_definition
SET priority = 4, datagrid_order_priority = 2, datagrid_order_by = 'ASC', in_title_and_priority = 2
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('tournament_administrator-tournament_administrator_main-tournament_administrator_member_autoinfo-firstname'));

UPDATE agenda_definition
SET priority = 3, datagrid_order_priority = 1, datagrid_order_by = 'ASC', in_title_and_priority = 1
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('tournament_administrator-tournament_administrator_main-tournament_administrator_member_autoinfo-surname'));

-- #14365 - all
UPDATE agenda SET is_dt_upd_in_datagrid = false WHERE is_dt_upd_in_datagrid = true;

UPDATE agenda_definition SET datagrid_priority = priority WHERE datagrid_priority IS NULL AND is_in_datagrid = true;

UPDATE agenda_definition SET datagrid_priority = 48
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('clubs-clubs_address-clubs_address_main-address_region'));

UPDATE agenda_definition SET datagrid_priority = 35
WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('clubs-clubs_main-clubs_main-identifier_idnumber'));

REFRESH MATERIALIZED VIEW mv_agenda_definition;
REFRESH MATERIALIZED VIEW mv_agenda_definition_property;
---------------
-- #14310

-- #14310 - 1)
INSERT INTO user_group_has_agenda_has_segment_group_definition (id_user_group, id_agenda_has_segment_has_attribute_group_definition, is_read, is_write)
SELECT usr.id, ahshagd.id, FALSE, FALSE
FROM (SELECT id
      FROM user_group
      WHERE name IN
            (
                'Clenove_Vse_Cteni+EditaceKyu'
                )) usr,
     agenda_has_segment_has_attribute_group_definition ahshagd
         JOIN agenda_has_segment_definition ahsd ON ahshagd.id_agenda_has_segment_definition = ahsd.id
         JOIN agenda_segment seg ON ahsd.id_agenda_segment = seg.id
         JOIN agenda_attribute_group grp ON ahshagd.id_agenda_attribute_group = grp.id
WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'members')
  AND (seg.name, grp.name) IN (('members_main', 'members_contact_main'), ('members_main', 'members_address_main'));

-- #14310 - 2)

INSERT INTO user_group_has_agenda_definition (id_user_group, id_agenda_definition, is_read, is_write)
SELECT grp.id AS id_user_group, ad.id as id_agenda_definition, false AS is_read, false AS is_write
FROM
    (SELECT id
     FROM user_group
     WHERE name IN
           ('Clenove_Vse_Cteni+EditaceKyu')) grp,
    (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN (
        'members-members_main-members_main-id_member',
        'members-members_main-members_main-foreigner',
        'members-members_main-members_main-gender',
        'members-members_main-members_main-identifier_value',
        'members-members_main-members_main-alternate_identifier',
        'members-members_main-members_main-dt_of_registration',
        'members-members_main-members_main-members_has_parents',
        'members-members_main-members_main-members_has_parents2',
        'members-members_main-members_main-active_fighter',
        'members-members_main-members_main-honorary_member',
        'members-members_main-members_main-suspended',
        'members-members_main-members_main-member_fee_current_year'
    )) ad;


REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_definition;
REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_has_segment_definition;
REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_has_segment_group_definition;
REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_permission;

-- #14896
INSERT INTO configuration.configuration_group (name, is_recurring, priority) VALUES ('hajime', false, 110);
INSERT INTO configuration.configuration_key (name, data_type, enabled) VALUES ('hajime_host', 'string', true);
INSERT INTO configuration.configuration_definition (name, id_configuration_group, id_configuration_key, is_read_only, is_not_null, priority) VALUES ('hajime_host', (SELECT id FROM configuration.configuration_group WHERE name = 'hajime'), (SELECT id FROM configuration.configuration_key WHERE name = 'hajime_host'), false, true, 10);

UPDATE flexii.master_lookup SET priority = 20 WHERE lookup_name = 'lookup_international_referee_class' AND name = 'ir_ijfa';
UPDATE flexii.master_lookup SET priority = 10 WHERE lookup_name = 'lookup_international_referee_class' AND name = 'ir_ijfb';


-- #14572
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--Události
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
INSERT INTO menu_section (name, icon, priority) VALUES('judo_event', NULL, 30);

-- menu
INSERT INTO agenda  (name, icon) VALUES('judo_event', 'fad fa-calendar-lines-pen');

INSERT INTO menu_link (name, presenter, action, parameters) VALUES('judo_event_default', 'Agenda:Agenda', 'default', '{"name":"judo_event"}');
INSERT INTO menu_link (name, presenter, action, parameters) VALUES('judo_event_detail', 'Agenda:Agenda', 'detail', '{"name":"judo_event"}');

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 = 'judo_event')
      , (select id from agenda where name = 'judo_event')
      , 'judo_event', 'fad fa-calendar-lines-pen'
      , (select id from menu_link where name = 'judo_event_default'), 6, false);

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


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


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

--tournaments_address_main
INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('judo_event_address', 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 = 'judo_event')
      , (select id from agenda_segment where name = 'judo_event_main')
      , 10, 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 = 'judo_event')
             and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_main')
          )
      , (select id from agenda_attribute_group where name = 'judo_event_main')
      , false, 1
      );

--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, datagrid_priority, is_search_by, in_title_and_priority,
                               id_agenda_segment_attribute_group_definition, max_length)
VALUES(
        (select id from attribute where name = 'name')
      , 10, true, NULL, false, false, NULL, 3, 0, 1, 10, true, 10
      , (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 = 'judo_event')
                and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_main')
          ), 100
      );

--Typ události
CREATE TABLE flexii.lookup_judo_event_type
(
    id          serial       NOT NULL,
    name        varchar(100) NOT NULL,
    description varchar(100),
    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_judo_event_type PRIMARY KEY (id)
);

ALTER TABLE lookup_judo_event_type
    OWNER TO flexii;

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

INSERT INTO lookup_judo_event_type (name, description, priority) VALUES ('seminar', 'Seminář', 10);
INSERT INTO lookup_judo_event_type (name, description, priority) VALUES ('training', 'Školení', 20);
INSERT INTO lookup_judo_event_type (name, description, priority) VALUES ('online_training', 'Online školení', 30);

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


--Typ události
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, datagrid_priority, is_search_by, in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'judo_event_type')
      , 20, true, NULL, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
                and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_main')
          )
      );

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES ('judo_event_status', 'Stav události', 0, 'lookup_judo_event_status', NULL, 1,
        (select id from attribute_data_type where name = 'lookup'));

INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES ('lookup_judo_event_status', 'planned', 'Plánován');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES ('lookup_judo_event_status', 'started', 'Započat');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES ('lookup_judo_event_status', 'ended', 'Ukončen');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES ('lookup_judo_event_status', 'canceled', 'Zrušen');

--Stav
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
                              , datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES ( (select id from attribute where name = 'judo_event_status')
       , 30, true
       , (SELECT id FROM master_lookup WHERE enabled = true AND lookup_name = 'lookup_judo_event_status' AND name = 'planned'), false, false, NULL, 3, 0, 1, 30, 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 = 'judo_event')
                and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_main')
          )
          and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_main'))
       );

--Datum konání od
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('dt_event_start', 'Datum konání od', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'datetime'));

--Datum konání 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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'dt_event_start')
      , 40, true, NULL, false, false, NULL, 3, 0, 1, 40, 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 = 'judo_event')
                and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_main')
          )
      );

--Datum konání do
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('dt_event_end', 'Datum konání do', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'datetime'));

--Datum konání 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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'dt_event_end')
      , 50, false, NULL, false, false, NULL, 3, 0, 1, 50, 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 = 'judo_event')
                and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'judo_event_main')
          )
      );

--Termín pro předlož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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'dt_term_for_registration')
      , 60, false, NULL, false, false, NULL, 3, 0, 1, 60, 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 = 'judo_event')
                and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_main')
          )
      );

--Webový link pro přihláš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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'web_link_for_registration')
      , 70, false, NULL, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
                and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'judo_event_main')
          )
      );

--Lokace
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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'locations')
      , 80, true, NULL, false, false, NULL, 3, 0, 1, 80, 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 = 'judo_event')
                    and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_main')
          )
      );

--Startovné
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('judo_event_starting_fee', 'Startovné', 0.01, NULL, NULL, 1, (select id from attribute_data_type where name = 'money'));

--Cena
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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'judo_event_starting_fee')
      , 90, true, 0.00, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
                    and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_main')
          )
      );
-- MAaximální počet účástní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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition, min_value)
VALUES(
          (select id from attribute where name = 'max_num_of_participants')
      , 1000, false, null, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
               and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_main')
          ), 0
      );

-- Adesa
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 = 'judo_event')
             and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_main')
          )
      , (select id from agenda_attribute_group where name = 'judo_event_address')
      , false, 1
      );

-- 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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'street')
      , 100, false, NULL, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
                    and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'judo_event_address')
          )
);

--Čí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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'street_number')
      , 110, true, NULL, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
                  and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'judo_event_address')
          )
      );

--Čí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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'house_number')
      , 120, false, NULL, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
                  and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'judo_event_address')
          )
      );

--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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'city')
      , 130, true, NULL, false, false, NULL, 3, 0, 1, 70, 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 = 'judo_event')
                  and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'judo_event_address')
          )
      );

--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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'postal_code')
      , 140, true, NULL, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
                  and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'judo_event_address')
          )
      );

--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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'address_region')
      , 150, true, NULL, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
                  and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'judo_event_address')
          )
      );

--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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'country')
      , 160, true, (select id from lookup_country where alpha2 = 'CZ'), false, true, NULL, 3, 0, 1, null, 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 = 'judo_event')
                  and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'judo_event_address')
          )
      );


-- -------------------------------
-- -- Záložka Informace o události
-- -------------------------------

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

--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 = 'judo_event')
      , (select id from agenda_segment where name = 'judo_event_information')
      , 20, false
      );

-- Segment Pořadatel
INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('judo_event_organizer', 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 = 'judo_event')
             and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_information')
          )
      , (select id from agenda_attribute_group where name = 'judo_event_organizer')
      , false, 1
      );

--
--Infomrace o události
--

-- Jméno
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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'firstname')
      , 170, true, NULL, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
                  and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_information')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_organizer')
          )
      );

-- Příjmení
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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'surname')
      , 180, true, NULL, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
                  and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_information')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_organizer')
          )
      );

-- Email
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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'email')
      , 190, true, NULL, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
                  and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_information')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_organizer')
          )
      );

-- Telefon
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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'telephone')
      , 200, true, NULL, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
                  and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_information')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_organizer')
          )
      );

--------------------------
-- Segment Odpovědná osoba
--------------------------
INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('judo_event_responsible_person', 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 = 'judo_event')
             and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_information')
          )
      , (select id from agenda_attribute_group where name = 'judo_event_responsible_person')
      , false, 2
      );

-- Odpovědná osoba

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 (
           'judo_event_has_responsible_person'
       , (select id from agenda where name = 'members')
       , (select id from agenda where name = 'judo_event')
       , 1, true, 290, false, false, false, false, true);

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


-- INSERT INTO ATTRIBUTE
-- (NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
-- VALUES('agenda_event_members', 'Pořadatel', 0, 'members', 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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'judo_event_has_responsible_person')
      , 210, true, NULL, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
                  and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_information')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_responsible_person')
          )
      );

-- Email
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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'email')
      , 220, true, NULL, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
                  and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_information')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_responsible_person')
          )
      );

-- Telefon
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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'telephone')
      , 230, true, NULL, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
                  and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_information')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_responsible_person')
          )
      );

------------------------------
-- Segment Všeobecné informace
------------------------------
INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('judo_event_information', 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 = 'judo_event')
             and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_information')
          )
      , (select id from agenda_attribute_group where name = 'judo_event_information')
      , false, 3
      );

-- Popis
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, datagrid_priority, is_search_by
                              , in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'description')
      , 240, false, NULL, false, false, NULL, 6, 0, 5, null, 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 = 'judo_event')
                    and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_information')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_information')
          )
      );

-- Datum zápisu do evidence
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, datagrid_priority, 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_of_registration')
      , 250, false, '@@current_date@@', false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
                    and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_information')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_information')
          )
      );

-- Datum změny v evidenci
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, datagrid_priority, 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_of_change')
      , 260, false, NULL, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
                    and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_information')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_information')
          )
      );

-- Aktivní
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, datagrid_priority, 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')
      , 270, true, true, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
                    and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_information')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_information')
          )
      );

-- --------------------
-- -- Záložka Propozice
-- --------------------

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

INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('judo_event_proposition', 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 = 'judo_event')
      , (select id from agenda_segment where name = 'judo_event_proposition')
      , 30, 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 = 'judo_event')
             and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_proposition')
          )
      , (select id from agenda_attribute_group where name = 'judo_event_proposition')
      , false, 1
      );

--Datum
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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'dt')
      , 280, true, NULL, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
              and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_proposition')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_proposition')
          )
      );

--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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'name')
      , 290, true, NULL, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
                    and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_proposition')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_proposition')
          )
      );

--Propozice
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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition, file_specification)
VALUES(
        (select id from attribute where name = 'proposition')
      , 300, true, NULL, false, false, NULL, 3, 0, 1, null, 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 = 'judo_event')
                    and id_agenda_segment = (select id from agenda_segment where name = 'judo_event_proposition')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'judo_event_proposition')
          ), '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'
      );

REFRESH MATERIALIZED VIEW mv_agenda_definition;

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, is_editable_agenda, is_editable_agenda_linked,
                               is_limited_by_permission, is_search_limited_by_permission,
                               is_search_limited_by_permission_linked, is_addable, is_addable_linked)
VALUES (
       'judo_event_has_members'
       , (select id from agenda where name = 'members')
       , (select id from agenda where name = 'judo_event')
       , 4, true, 280, false, false, false, false, true, false, true, true, true, true, false, false);

-- Klub
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 = 'judo_event_has_members')
    , (SELECT id FROM mv_agenda_definition
           WHERE agenda_name = 'members'
               AND segment_name = 'members_main'
               AND attribute_group_name ='members_main'
               AND attribute_name = 'clubs_has_members'
       )
    , false
);
-- Typ události
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 = 'judo_event_has_members')
       , (SELECT id FROM mv_agenda_definition
          WHERE agenda_name = 'judo_event'
            AND segment_name = 'judo_event_main'
            AND attribute_group_name ='judo_event_main'
            AND attribute_name = 'judo_event_type'
           )
       , 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 = 'judo_event_has_members')
       , (SELECT id FROM mv_agenda_definition
          WHERE agenda_name = 'judo_event'
            AND segment_name = 'judo_event_main'
            AND attribute_group_name ='judo_event_main'
            AND attribute_name = 'judo_event_status'
           )
       , false
       );

-- Země
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 = 'judo_event_has_members')
       , (SELECT id FROM mv_agenda_definition
          WHERE agenda_name = 'judo_event'
            AND segment_name = 'judo_event_main'
            AND attribute_group_name ='judo_event_address'
            AND attribute_name = 'country'
           )
       , false
       );

REFRESH MATERIALIZED VIEW mv_agenda_definition_property;
REFRESH MATERIALIZED VIEW mv_agenda_has_agenda_definition_property;

-- #14572 - permission
INSERT INTO base.user_group (description, name) VALUES
    ('Události - vše - čtení', 'Udalosti_Vse_Cteni'),
    ('Události - vše - čtení a zápis', 'Udalosti_Vse_Cteni_Zapis'),
    ('Události - vše - čtení a registrace', 'Udalosti_Vse_Cteni_Registrace');

INSERT INTO user_group_has_agenda (id_user_group, id_agenda, is_can_read, is_can_write, is_can_create, is_can_delete)
VALUES ((SELECT id FROM user_group WHERE name = 'Udalosti_Vse_Cteni'), (SELECT id FROM agenda WHERE name = 'judo_event'), TRUE, FALSE, FALSE, false),
       ((SELECT id FROM user_group WHERE name = 'Udalosti_Vse_Cteni_Zapis'), (SELECT id FROM agenda WHERE name = 'judo_event'), TRUE, true, true, true),
       ((SELECT id FROM user_group WHERE name = 'Udalosti_Vse_Cteni_Registrace'), (SELECT id FROM agenda WHERE name = 'judo_event'), TRUE, false, FALSE, false);

INSERT INTO user_group_has_agenda (id_user_group, id_agenda, is_can_read, is_can_write, is_can_create, is_can_delete)
values ((select id from base.user_group where name = 'ext_esports'), (select id from agenda where name = 'judo_event'), true, false, false, false);


INSERT INTO user_group_has_agenda_has_agenda (id_user_group, id_agenda_has_agenda, is_can_read, is_can_write, is_can_create, is_can_delete)
VALUES ((SELECT id FROM user_group WHERE name = 'ext_esports'), (SELECT id FROM agenda_has_agenda WHERE name = 'judo_event_has_members'), TRUE, FALSE, FALSE, false);

INSERT INTO user_group_has_agenda_has_agenda (id_user_group, id_agenda_has_agenda, is_can_read, is_can_write, is_can_create, is_can_delete)
VALUES ((SELECT id FROM user_group WHERE name = 'Udalosti_Vse_Cteni'), (SELECT id FROM agenda_has_agenda WHERE name = 'judo_event_has_members'), TRUE, FALSE, FALSE, false),
       ((SELECT id FROM user_group WHERE name = 'Udalosti_Vse_Cteni_Zapis'), (SELECT id FROM agenda_has_agenda WHERE name = 'judo_event_has_members'), TRUE, true, true, true),
       ((SELECT id FROM user_group WHERE name = 'Udalosti_Vse_Cteni_Registrace'), (SELECT id FROM agenda_has_agenda WHERE name = 'judo_event_has_members'), TRUE, false, FALSE, false);

REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_definition;
REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_has_segment_definition;
REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_has_segment_group_definition;
REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_permission;
REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_has_agenda;

UPDATE flexii.agenda_has_agenda SET is_addable = false, is_addable_linked = false WHERE name = 'judo_event_has_members';

----------------------------------------------------
-- 2.6.16.0
-- SQL for this version ends here
----------------------------------------------------


-- #14630
DELETE
FROM user_group_has_agenda_definition
WHERE id_user_group = (SELECT id FROM user_group WHERE name = 'Kluby_AdminemKlubu_EditLimitovan+ObjednaniPoplatku+TiskKaret')
  AND id_agenda_definition IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN (
                                                                                              'clubs-clubs_main-clubs_main-account_number',
                                                                                              'clubs-clubs_main-clubs_main-description',
                                                                                              'clubs-clubs_main-clubs_main-web_page',
                                                                                              'clubs-clubs_main-clubs_main-telephone_2',
                                                                                              'clubs-clubs_main-clubs_main-electronic_application',
                                                                                              'clubs-clubs_main-clubs_main-electronic_application_url'
    ));

REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_definition;
REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_has_segment_definition;
REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_has_segment_group_definition;
REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_permission;
REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_has_agenda;

-- #14366
UPDATE agenda_values SET value = REPLACE(value, '/', '') WHERE id_agenda_definition = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-identifier_value');

INSERT INTO flexii.agenda_definition_validation (id_agenda_definition, class_name, priority)
VALUES ((SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-identifier_value'), 'WithoutSlashValidation', 5);

INSERT INTO flexii.agenda_definition_validation (id_agenda_definition, class_name, priority)
VALUES ((SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-identifier_value'), 'RcValidation', 10);


-- #14631

-- --záložka Stanovy

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

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

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_sport_fields')
      , 50, 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 = 'clubs')
             and id_agenda_segment = (select id from agenda_segment where name = 'club_sport_fields')
          )
      , (select id from agenda_attribute_group where name = 'club_sport_fields_main')
      , 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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'name')
      , 10, true, NULL, false, false, NULL, 3, 0, 1, null, 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_sport_fields')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_sport_fields_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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'street')
      , 20, true, NULL, false, false, NULL, 3, 0, 1, null, 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_sport_fields')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_sport_fields_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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'street_number')
      , 30, true, NULL, false, false, NULL, 3, 0, 1, null, 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_sport_fields')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_sport_fields_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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'house_number')
      , 40, false, NULL, false, false, NULL, 3, 0, 1, null, 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_sport_fields')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_sport_fields_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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'city')
      , 50, true, NULL, false, false, NULL, 3, 0, 1, null, 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_sport_fields')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_sport_fields_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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'city_area')
      , 60, false, NULL, false, false, NULL, 3, 0, 1, null, 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_sport_fields')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_sport_fields_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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'postal_code')
      , 70, true, NULL, false, false, NULL, 3, 0, 1, null, 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_sport_fields')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_sport_fields_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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'address_region')
      , 80, true, NULL, false, false, NULL, 3, 0, 1, null, 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_sport_fields')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_sport_fields_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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'country')
      , 90, true, NULL, false, false, NULL, 3, 0, 1, null, 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_sport_fields')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_sport_fields_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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'club_gps_x')
      , 100, true, NULL, false, false, NULL, 3, 0, 1, null, 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_sport_fields')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_sport_fields_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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'club_gps_y')
      , 110, true, NULL, false, false, NULL, 3, 0, 1, null, 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_sport_fields')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_sport_fields_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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'is_active')
      , 120, true, 1, false, false, NULL, 3, 0, 1, null, 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_sport_fields')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_sport_fields_main')
          )
      );

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('long_note', 'Dlouhá poznámka', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'long_text'));

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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'long_note')
      , 130, false, NULL, false, false, NULL, 3, 0, 1, null, 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_sport_fields')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_sport_fields_main')
          )
      );

INSERT into user_group_has_agenda_has_segment_definition (id_user_group, id_agenda_has_segment_definition, is_read, is_write)
VALUES ((select id from base.user_group where name = 'Kluby_Vse_Edit+Pridani+TiskKaret'),
        (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_sport_fields')),
        true,
        false);

refresh materialized view mv_user_group_has_agenda_definition;
refresh materialized view mv_user_group_has_agenda_has_segment_definition;
refresh materialized view mv_user_group_has_agenda_has_segment_group_definition;
refresh materialized view mv_user_group_has_agenda_permission;

---- #15007
---- 3)
UPDATE agenda_has_agenda_show_attribute SET datagrid_order_priority = 10, datagrid_order_by = 'DESC'
WHERE id_agenda_has_agenda = (SELECT id
                              FROM agenda_has_agenda
                              WHERE name = 'clubs_has_union_fees')
  AND id_agenda_definition = (SELECT id
                              FROM mv_agenda_definition
                              WHERE unique_name = 'union_fees_main-union_fees_main-dt_of_payment_issue');

---- #15007
---- 4)
INSERT INTO user_group_has_appuser (id_user_group, id_appuser)
SELECT id_user_group, id_appuser FROM (
                                          SELECT DISTINCT VALUE::INT AS id_appuser
                                          FROM agenda_values
                                          WHERE id_agenda_definition =
                                                (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'club_administrator-club_administrator_main-club_administrator_main-appuser')
                                            AND VALUE::INT IN (SELECT id_appuser
                                                               FROM user_group_has_appuser
                                                               WHERE id_user_group = (SELECT id FROM user_group WHERE NAME = 'Kluby_AdminemKlubu_EditLimitovan+ObjednaniPoplatku+TiskKaret')
                                                                 AND ENABLED = TRUE)) usr,
                                      (SELECT id AS id_user_group
                                       FROM user_group
                                       WHERE name = 'Udalosti_Vse_Cteni_Registrace') grp;

---- #15007
---- 5)
UPDATE agenda_definition SET priority = 15 WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-surname');
UPDATE agenda_definition SET priority = 20 WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-firstname');
UPDATE agenda_definition SET priority = 25 WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-gender');
REFRESH MATERIALIZED VIEW mv_agenda_definition;

---- #15007
---- 2)
UPDATE agenda_has_agenda_show_attribute
SET datagrid_column_priority = 10
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 full_unique_name = 'members-members_main-members_main-id_member');

UPDATE agenda_has_agenda_show_attribute
SET datagrid_column_priority = 15
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 full_unique_name = 'members-members_main-members_main-surname');

UPDATE agenda_has_agenda_show_attribute
SET datagrid_column_priority = 20
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 full_unique_name = 'members-members_main-members_main-firstname');

UPDATE agenda_has_agenda_show_attribute
SET datagrid_column_priority = 25
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 full_unique_name = 'members-members_main-members_main-dt_birthdate');

UPDATE agenda_has_agenda_show_attribute
SET datagrid_column_priority = 30
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 full_unique_name = 'members-members_main-members_main-member_fee_current_year');

UPDATE agenda_has_agenda_show_attribute
SET datagrid_column_priority = 35
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 full_unique_name = 'members-members_main-members_main-age_category_text');

----------------------------------------------------
-- 2.6.17.0
-- SQL for this version ends here
----------------------------------------------------

-- #15042
UPDATE agenda_definition
SET is_unique = true
WHERE id = (
    SELECT id FROM mv_agenda_definition
        WHERE agenda_name = 'examiner'
          AND segment_name = 'examiner_main'
          AND attribute_group_name = 'examiner_main'
          AND attribute_name = 'appuser'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

----------------------------------------------------
-- 2.6.17.1
-- SQL for this version ends here
----------------------------------------------------


---------------------------
-------- #14413 -----------

INSERT INTO master_lookup (lookup_name, name, description, priority) VALUES ('lookup_states_of_payment', 'error', 'Chyba', 40);

INSERT INTO agenda_action (name, description, class_name, is_via_rabbit) VALUES ('UnionFeesSendData', 'Zapsání poplatku do Ježek SW DUEL', 'UnionFeesSendData', true);
INSERT INTO agenda_button (id_agenda, name, description, icon, class, confirmation)
VALUES ((SELECT id FROM agenda WHERE name = 'union_fees'), 'UnionFeeRepeat', 'Opakovat', 'fal fa-repeat', 'btn-info', null);

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

UPDATE agenda_button SET enabled = false WHERE name IN ('UnionFeePayment','UnionFeeCancel');

UPDATE agenda_has_agenda SET is_deletable = false, is_deletable_linked = false WHERE name = 'members_has_union_fees';

INSERT INTO agenda_action (name, description, class_name, is_via_rabbit) VALUES ('UnionFeeStatus', 'Zjištění stavu poplatku z Ježek SW DUEL', 'UnionFeeStatus', true);
INSERT INTO agenda_button (id_agenda, name, description, icon, class, confirmation)
VALUES ((SELECT id FROM agenda WHERE name = 'union_fees'), 'UnionFeeStatus', 'Zjistit stav', 'fal fa-repeat', 'btn-info', null);

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

insert into configuration.configuration_group (name, is_recurring, priority) VALUES ('csju_mssql_db_duel_server', false, 120);

insert into configuration.configuration_key (name, data_type) VALUES ('duel_mssql_host', 'string'),
                                                                     ('duel_mssql_port', 'string'),
                                                                     ('duel_mssql_user', 'string'),
                                                                     ('duel_mssql_password', 'password');

insert into configuration.configuration_definition (name, id_configuration_group, id_configuration_key, is_not_null, default_value, example, note, is_read_only, sequence_mandatory, priority) VALUES
('duel_mssql_host', (select id from configuration.configuration_group where name = 'csju_mssql_db_duel_server'), (select id from configuration.configuration_key where name = 'duel_mssql_host'), false, null, '', '', false, null::smallint, 110),
('duel_mssql_port', (select id from configuration.configuration_group where name = 'csju_mssql_db_duel_server'), (select id from configuration.configuration_key where name = 'duel_mssql_port'), false, null, '', '', false, null::smallint, 120),
('duel_mssql_user', (select id from configuration.configuration_group where name = 'csju_mssql_db_duel_server'), (select id from configuration.configuration_key where name = 'duel_mssql_user'), false, null, '', '', false, null::smallint, 140),
('duel_mssql_password', (select id from configuration.configuration_group where name = 'csju_mssql_db_duel_server'), (select id from configuration.configuration_key where name = 'duel_mssql_password'), false, null, '', '', false, null::smallint, 150);

insert into configuration.configuration_group (name, is_recurring, priority) VALUES ('csju_duel_static_params', false, 130);

insert into configuration.configuration_key (name, data_type) VALUES ('duel_firma_uid', 'string'),
                                                                     ('duel_mena_zkratka', 'string'),
                                                                     ('duel_splatnost', 'int');

insert into configuration.configuration_definition (name, id_configuration_group, id_configuration_key, is_not_null, default_value, example, note, is_read_only, sequence_mandatory, priority) VALUES
   ('duel_firma_uid', (select id from configuration.configuration_group where name = 'csju_duel_static_params'), (select id from configuration.configuration_key where name = 'duel_firma_uid'), true, null, '', 'Sloupec UID z tabulky DuelSystem.dbo.Firma', false, null::smallint, 10),
   ('duel_mena_zkratka', (select id from configuration.configuration_group where name = 'csju_duel_static_params'), (select id from configuration.configuration_key where name = 'duel_mena_zkratka'), true, null, '', 'Sloupec ZKRATKA z tabulky DuelFirma_*.dbo.Mena', false, null::smallint, 20),
   ('duel_splatnost', (select id from configuration.configuration_group where name = 'csju_duel_static_params'), (select id from configuration.configuration_key where name = 'duel_splatnost'), true, 14, '', 'Výchozí splatnost faktur', false, null::smallint, 30);

insert into configuration.configuration_group (name, is_recurring, priority) VALUES ('csju_duel_zakazka', true, 140);

insert into configuration.configuration_key (name, data_type) VALUES ('duel_lookup_region_name', 'string'),
                                                                     ('duel_zakazka_zkratka', 'string');

insert into configuration.configuration_definition (name, id_configuration_group, id_configuration_key, is_not_null, default_value, example, note, is_read_only, sequence_mandatory, priority) VALUES
   ('duel_lookup_region_name', (select id from configuration.configuration_group where name = 'csju_duel_zakazka'), (select id from configuration.configuration_key where name = 'duel_lookup_region_name'), true, null, '', 'Název hodnoty číselníku lookup_locations', false, null::smallint, 10),
   ('duel_zakazka_zkratka', (select id from configuration.configuration_group where name = 'csju_duel_zakazka'), (select id from configuration.configuration_key where name = 'duel_zakazka_zkratka'), true, null, '', 'Sloupec ZKRATKA z tabulky DuelFirma_*.dbo.Zakazka', false, null::smallint, 20);


-- region_1,Hlavní město Praha
INSERT into configuration.configuration_values (id_configuration_definition, value, sequence_priority)
VALUES
        ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_lookup_region_name'), 'region_1', 1),
       ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_zakazka_zkratka'), '18001', 1);
-- region_2,Středočeský kraj
INSERT into configuration.configuration_values (id_configuration_definition, value, sequence_priority)
VALUES
        ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_lookup_region_name'), 'region_2', 2),
       ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_zakazka_zkratka'), '18002', 2);
-- region_3,Jihočeský kraj
INSERT into configuration.configuration_values (id_configuration_definition, value, sequence_priority)
VALUES
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_lookup_region_name'), 'region_3', 3),
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_zakazka_zkratka'), '18003', 3);
-- region_4,Plzeňský kraj
INSERT into configuration.configuration_values (id_configuration_definition, value, sequence_priority)
VALUES
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_lookup_region_name'), 'region_4', 4),
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_zakazka_zkratka'), '18004', 4);
-- region_5,Karlovarský kraj
INSERT into configuration.configuration_values (id_configuration_definition, value, sequence_priority)
VALUES
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_lookup_region_name'), 'region_5', 5),
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_zakazka_zkratka'), '18005', 5);
-- region_6,Ústecký kraj
INSERT into configuration.configuration_values (id_configuration_definition, value, sequence_priority)
VALUES
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_lookup_region_name'), 'region_6', 6),
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_zakazka_zkratka'), '18006', 6);
-- region_7,Liberecký kraj
INSERT into configuration.configuration_values (id_configuration_definition, value, sequence_priority)
VALUES
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_lookup_region_name'), 'region_7', 7),
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_zakazka_zkratka'), '18007', 7);
-- region_8,Královéhradecký kraj
INSERT into configuration.configuration_values (id_configuration_definition, value, sequence_priority)
VALUES
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_lookup_region_name'), 'region_8', 8),
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_zakazka_zkratka'), '18008', 8);
-- region_9,Pardubický kraj
INSERT into configuration.configuration_values (id_configuration_definition, value, sequence_priority)
VALUES
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_lookup_region_name'), 'region_9', 9),
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_zakazka_zkratka'), '18009', 9);
-- region_10,Kraj Vysočina
INSERT into configuration.configuration_values (id_configuration_definition, value, sequence_priority)
VALUES
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_lookup_region_name'), 'region_10', 10),
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_zakazka_zkratka'), '18010', 10);
-- region_11,Jihomoravský kraj
INSERT into configuration.configuration_values (id_configuration_definition, value, sequence_priority)
VALUES
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_lookup_region_name'), 'region_11', 11),
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_zakazka_zkratka'), '18011', 11);
-- region_12,Olomoucký kraj
INSERT into configuration.configuration_values (id_configuration_definition, value, sequence_priority)
VALUES
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_lookup_region_name'), 'region_12', 12),
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_zakazka_zkratka'), '18013', 12);
-- region_13,Zlínský kraj
INSERT into configuration.configuration_values (id_configuration_definition, value, sequence_priority)
VALUES
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_lookup_region_name'), 'region_13', 13),
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_zakazka_zkratka'), '18012', 13);
-- region_14,Moravskoslezský kraj
INSERT into configuration.configuration_values (id_configuration_definition, value, sequence_priority)
VALUES
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_lookup_region_name'), 'region_14', 14),
    ((SELECT id FROM configuration.configuration_definition WHERE name = 'duel_zakazka_zkratka'), '18014', 14);



insert into configuration.configuration_group (name, is_recurring, priority) VALUES ('csju_duel_time_params', true, 150);

insert into configuration.configuration_key (name, data_type) VALUES ('duel_doklad_zkratka', 'string'),
                                                                     ('duel_doklad_postfix', 'string'),
                                                                     ('duel_stredisko_zkratka', 'string'),
                                                                     ('duel_druh_uctovani_zkratka', 'string'),
                                                                     ('dt_valid_from', 'string'),
                                                                     ('dt_valid_to', 'string');

insert into configuration.configuration_definition (name, id_configuration_group, id_configuration_key, is_not_null, default_value, example, note, is_read_only, sequence_mandatory, priority) VALUES
                   ('duel_doklad_zkratka', (select id from configuration.configuration_group where name = 'csju_duel_time_params'), (select id from configuration.configuration_key where name = 'duel_doklad_zkratka'), true, null, 'fvc', 'Sloupec ZKRATKA z tabulky DuelFirma_*.dbo.Doklad', false, null::smallint, 10),
                   ('duel_doklad_postfix', (select id from configuration.configuration_group where name = 'csju_duel_time_params'), (select id from configuration.configuration_key where name = 'duel_doklad_postfix'), true, null, '2024', 'Sloupec DOKLAD_POSTFIX z tabulky DuelFirma_*.dbo.Doklad', false, null::smallint, 20),
                   ('duel_stredisko_zkratka', (select id from configuration.configuration_group where name = 'csju_duel_time_params'), (select id from configuration.configuration_key where name = 'duel_stredisko_zkratka'), true, null, '24001', 'Sloupec ZKRATKA z tabulky DuelFirma_*.dbo.Stredisko', false, null::smallint, 30),
                   ('duel_druh_uctovani_zkratka', (select id from configuration.configuration_group where name = 'csju_duel_time_params'), (select id from configuration.configuration_key where name = 'duel_druh_uctovani_zkratka'), true, null, 'CP24', 'Sloupec ZKRATKA z tabulky DuelFirma_*.dbo.DruhUctovani', false, null::smallint, 40),
                   ('dt_valid_from', (select id from configuration.configuration_group where name = 'csju_duel_time_params'), (select id from configuration.configuration_key where name = 'dt_valid_from'), true, null, '01.01.2024', 'Platnost od ve fromátu DD.MM.YYYY', false, null::smallint, 50),
                   ('dt_valid_to', (select id from configuration.configuration_group where name = 'csju_duel_time_params'), (select id from configuration.configuration_key where name = 'dt_valid_to'), true, null, '31.12.2024', 'Platnost do ve fromátu DD.MM.YYYY', false, null::smallint, 60);

----------------------------------------------------
-- 2.6.18.0
-- SQL for this version ends here
----------------------------------------------------

-- #14240

UPDATE lookup_kyu_level
SET priority = CASE
                   WHEN name = 'kyu1' THEN 5
                   WHEN name = 'kyu2' THEN 15
                   WHEN name = 'kyu3_2' THEN 25
                   WHEN name = 'kyu3' THEN 35
                   WHEN name = 'kyu4_3' THEN 45
                   WHEN name = 'kyu4' THEN 55
                   WHEN name = 'kyu5_4' THEN 65
                   WHEN name = 'kyu5' THEN 85
                   WHEN name = 'kyu6_5' THEN 95
                   WHEN name = 'kyu6' THEN 100

    END
WHERE name IN ('kyu1', 'kyu2', 'kyu3_2', 'kyu3', 'kyu4_3', 'kyu4', 'kyu5_4', 'kyu5', 'kyu6_5', 'kyu6');

UPDATE master_lookup
SET priority = CASE
                   WHEN name = 'dan10' THEN 5
                   WHEN name = 'dan9' THEN 15
                   WHEN name = 'dan8' THEN 25
                   WHEN name = 'dan7' THEN 35
                   WHEN name = 'dan6' THEN 45
                   WHEN name = 'dan5' THEN 55
                   WHEN name = 'dan4' THEN 65
                   WHEN name = 'dan3' THEN 75
                   WHEN name = 'dan2' THEN 85
                   WHEN name = 'dan1' THEN 95

    END
WHERE name IN ('dan1', 'dan2', 'dan3', 'dan4', 'dan5', 'dan6', 'dan7', 'dan8', 'dan9', 'dan10')
AND lookup_name = 'lookup_dan_level';



--#14866
INSERT INTO agenda_action (name, description, class_name, is_via_rabbit)
VALUES ('TournamentClose', 'Uzavřít turnaj', 'TournamentCloseAction', false);

INSERT INTO agenda_button (id_agenda, name, description, icon, class)
VALUES ((SELECT id FROM agenda WHERE name = 'tournaments'), 'TournamentClose', 'Uzavřít turnaj', 'fal fa-lock', 'btn-danger');

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


--#14889
--Nový atribut
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('dt_term_for_registration_start', 'Termín zahájení přihllášení ', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'datetime'));

--Termín pro zahájení přihláš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_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'dt_term_for_registration_start'),
          33, true, NULL, false, false, NULL, 3, 0, 1, false, NULL,
          (select id_agenda_segment_attribute_group_definition from mv_agenda_definition WHERE agenda_name='tournaments' AND segment_name='tournaments_main' AND attribute_group_name='tournaments_main' LIMIT 1)
      );

REFRESH MATERIALIZED VIEW mv_agenda_definition;
REFRESH MATERIALIZED VIEW mv_agenda_definition_property;

--validace - termin pro zahájení musi byt menší, nez termin ukončení

INSERT INTO agenda_definition_validation_by_operator (id_agenda_definition, operator, id_agenda_definition_second)
VALUES ((SELECT id
         from mv_agenda_definition
         WHERE agenda_name = 'tournaments'
           AND segment_name = 'tournaments_main'
           AND attribute_group_name = 'tournaments_main'
           AND attribute_name = 'dt_term_for_registration_start'),
        '<',
        (SELECT id
         from mv_agenda_definition
         WHERE agenda_name = 'tournaments'
           AND segment_name = 'tournaments_main'
           AND attribute_group_name = 'tournaments_main'
           AND attribute_name = 'dt_term_for_registration')
       );




--#14890
INSERT INTO attribute (name, description, lookup_columns, id_attribute_data_type, custom_data_class)
VALUES ('results', 'Tournament results', null, (SELECT id FROM attribute_data_type WHERE name='file'), null);

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

INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('tournament_results_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 = 'tournaments')
      , (select id from agenda_segment where name = 'tournament_results')
      , 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 = 'tournaments')
             and id_agenda_segment = (select id from agenda_segment where name = 'tournament_results')
          )
      , (select id from agenda_attribute_group where name = 'tournament_results_main')
      , false, 5
      );

--Datum
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_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'dt')
      , 10, true, '@@current_date@@', false, false, NULL, 3, 0, 1, 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_results')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'tournament_results_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_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition, max_length)
VALUES(
          (select id from attribute where name = 'name')
      , 20, true, NULL, false, false, NULL, 3, 0, 1, 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_results')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'tournament_results_main')
          ),
          100
      );

--Propozice
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_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition, file_specification)
VALUES(
          (select id from attribute where name = 'results')
      , 30, true, NULL, false, false, NULL, 3, 0, 1, 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_results')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'tournament_results_main')
          )
      ,  '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'
      );

--Restrict result writing for all the dumb regional tournament user groups
INSERT INTO user_group_has_agenda_has_segment_group_definition (id_user_group, id_agenda_has_segment_has_attribute_group_definition, is_read, is_write)
    (SELECT
         id    as id_user_group,
         (
             SELECT id FROM agenda_has_segment_has_attribute_group_definition
             WHERE id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_results_main')
         )
               as id_agenda_has_segment_has_attribute_group_definition,
         true  as is_read,
         false as is_write
     FROM base.user_group
     WHERE name IN
           ('Turnaje_JihoceskyKraj_Edit+Pridani', 'Turnaje_JihomoravskyKraj_Edit+Pridani',
            'Turnaje_KarlovarskyKraj_Edit+Pridani', 'Turnaje_KralovehradeckyKraj_Edit+Pridani',
            'Turnaje_LibereckyKraj_Edit+Pridani', 'Turnaje_MoravskoslezskyKraj_Edit+Pridani',
            'Turnaje_OlomouckyKraj_Edit+Pridani', 'Turnaje_PardubickyKraj_Edit+Pridani',
            'Turnaje_PlzenskyKraj_Edit+Pridani', 'Turnaje_Praha_Edit+Pridani',
            'Turnaje_StredoceskyKraj_Edit+Pridani', 'Turnaje_UsteckyKraj_Edit+Pridani',
            'Turnaje_Vysocina_Edit+Pridani', 'Turnaje_ZlinskyKraj_Edit+Pridani',
            'Turnaje_Vse_Edit+Pridani')
    );
--ext_mups cant read results
INSERT INTO user_group_has_agenda_has_segment_group_definition (id_user_group, id_agenda_has_segment_has_attribute_group_definition, is_read, is_write)
VALUES (
    (SELECT id FROM base.user_group WHERE name='ext_mups'),
    (
        SELECT id FROM agenda_has_segment_has_attribute_group_definition
        WHERE id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_results_main')
    ),
    false, false
);

INSERT INTO user_group_has_agenda_has_segment_group_definition (id_user_group, id_agenda_has_segment_has_attribute_group_definition, is_read, is_write)
VALUES (
   (SELECT id FROM base.user_group WHERE name='ext_esports'),
   (
       SELECT id FROM agenda_has_segment_has_attribute_group_definition
       WHERE id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_results_main')
   ),
   true, false
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;
REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_has_segment_group_definition;


--#14982
--let clenove_vse_cteni+editacekyu read member fee status + age category text
INSERT INTO user_group_has_agenda_definition (id_user_group, id_agenda_definition, is_read, is_write)
SELECT
    (SELECT id FROM base.user_group WHERE name = 'Clenove_Vse_Cteni+EditaceKyu') as id_user_group,
    id as id_agenda_definition,
    TRUE as is_read,
    FALSE as is_write
FROM mv_agenda_definition
WHERE agenda_name='members'
  AND segment_name='members_main'
  AND attribute_group_name='members_main'
  AND mv_agenda_definition.attribute_name IN ('member_fee_current_year', 'age_category_text')
ON CONFLICT(id_user_group, id_agenda_definition) DO UPDATE SET is_read=true, is_write=false;

REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_definition;

----------------------------------------------------
-- 2.6.19.0
-- SQL for this version ends here
----------------------------------------------------


-- -------------------------------
-- -- Záložka KATA - rozhodčí
-- -------------------------------

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

--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 = 'referee')
      , (select id from agenda_segment where name = 'referee_kata')
      , 40, false
      );

-- Segment Pořadatel
INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('referee_kata', true, 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 = 'referee')
             and id_agenda_segment = (select id from agenda_segment where name = 'referee_kata')
          )
      , (select id from agenda_attribute_group where name = 'referee_kata')
      , false, 1
      );

--
--Kategorie KATA
--

--Role funkcionáře klubu
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('kata_category', 'Kategoreie KATA', 0, 'lookup_kata_category', NULL, 1, (select id from attribute_data_type where name = 'lookup'));

INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES('lookup_kata_category', 'nage_no_kata', 'Nage-no-Kata');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES('lookup_kata_category', 'katame_no_kata', 'Katame-no-Kata');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES('lookup_kata_category', 'kime_no_kata', 'Kime-no-Kata');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES('lookup_kata_category', 'kodokan_goshin_jutsu', 'Kodokan-Goshin-jutsu');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES('lookup_kata_category', 'ju_no_kata', 'Ju-no-Kata');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES('lookup_kata_category', 'itsutsu_no_kata', 'Itsutsu-no-Kata');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES('lookup_kata_category', 'koshiki_no_kata', 'Koshiki-no-Kata');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES('lookup_kata_category', 'seiryoku_zenyo_kokumin_taiiku', 'Seiryoku-Zenyo-Kokumin-Taiiku');

-- Kategorie kata
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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'kata_category')
      , 10, true, NULL, false, false, NULL, 3, 0, 1, null, 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 = 'referee')
                  and id_agenda_segment = (select id from agenda_segment where name = 'referee_kata')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'referee_kata')
          )
      );

--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, datagrid_priority, 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')
      , 20, true, NULL, false, false, NULL, 3, 0, 1, NULL, 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 = 'referee')
            and id_agenda_segment = (select id from agenda_segment where name = 'referee_kata')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'referee_kata')
          )
      );

--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, datagrid_priority, 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')
      , 30, true, NULL, false, false, NULL, 3, 0, 1, NULL, 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 = 'referee')
            and id_agenda_segment = (select id from agenda_segment where name = 'referee_kata')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'referee_kata')
          )
      );

INSERT INTO admin_editable_lookup (name, is_master_lookup, enabled) values (
 'lookup_kata_category', true, true
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- added validation to dt_validity_from < dt_validity_to
INSERT INTO agenda_definition_validation_by_operator (id_agenda_definition, operator, id_agenda_definition_second)
VALUES ((SELECT id
         from mv_agenda_definition
         WHERE agenda_name = 'referee'
           AND segment_name = 'referee_kata'
           AND attribute_group_name = 'referee_kata'
           AND attribute_name = 'dt_validity_from'),
        '<',
        (SELECT id
         from mv_agenda_definition
         WHERE agenda_name = 'referee'
           AND segment_name = 'referee_kata'
           AND attribute_group_name = 'referee_kata'
           AND attribute_name = 'dt_validity_to')
        );

INSERT INTO agenda_has_agenda_show_attribute_group (id_agenda_has_agenda, id_agenda_has_segment_has_attribute_group_definition)
VALUES ((SELECT id FROM agenda_has_agenda WHERE name = 'members_has_referee'),
        (SELECT id FROM agenda_has_segment_has_attribute_group_definition
         WHERE id_agenda_attribute_group = (SELECT id FROM agenda_attribute_group WHERE name = 'referee_kata')
           AND id_agenda_has_segment_definition = (
           SELECT id FROM agenda_has_segment_definition
               WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'referee')
                 AND id_agenda_segment = (SELECT id FROM agenda_segment WHERE name = 'referee_kata')
           )));

-- #14981

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_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'email')
      , 50, false, NULL, false, true, NULL, 3, 0, 1, true, 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 = 'referee')
               and id_agenda_segment = (select id from agenda_segment where name = 'referee_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'referee_member_autoinfo')
          )
      );


refresh materialized view mv_agenda_definition;

INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES('member_referee-email_member_email_member'
      , (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'
       )
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'referee'
           AND segment_name = 'referee_main'
           AND attribute_group_name = 'referee_member_autoinfo'
           AND attribute_name = 'email'
       )
      );

INSERT INTO sync_has_mapping (id_agenda_to_agenda_sync, id_agenda_definition_to_agenda_definition_mapping)
VALUES (
           (SELECT id FROM agenda_to_agenda_sync WHERE name = 'members_has_referee_agenda_sync')
       , (SELECT id FROM agenda_definition_to_agenda_definition_mapping WHERE name = 'member_referee-email_member_email_member')
       );

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_search_by,max_length, id_agenda_segment_attribute_group_definition, datagrid_priority)
VALUES(
          (select id from attribute where name = 'locations')
      , 150, false, NULL, false, true, NULL, 3, 0, 1, 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 = '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')
          ),78
      );

refresh materialized view mv_agenda_definition;

INSERT INTO agenda_to_agenda_sync
(name, id_agenda_has_agenda, id_agenda_definition_control, agenda_value_control, is_limited_to_primary, is_create_entity_if_relation_not_exist, is_sync_both_ways, id_agenda_sync_source)
VALUES ( 'clubs_has_members_agenda_sync'
       , (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members')
       , NULL
       , NULL
       , true
       , false
       , false
       , (select id FROM agenda WHERE name = 'clubs')
       );

INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES('member_club-club_location_club_location',
      (SELECT id FROM mv_agenda_definition
             WHERE agenda_name = 'members'
               AND segment_name = 'members_main'
               AND attribute_group_name = 'members_main'
               AND attribute_name = 'locations'
       )
      , (SELECT 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 sync_has_mapping (id_agenda_to_agenda_sync, id_agenda_definition_to_agenda_definition_mapping)
VALUES (
           (SELECT id FROM agenda_to_agenda_sync WHERE name = 'clubs_has_members_agenda_sync')
       , (SELECT id FROM agenda_definition_to_agenda_definition_mapping WHERE name = 'member_club-club_location_club_location')
       );


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_search_by,max_length, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'locations')
      , 150, false, NULL, false, true, NULL, 3, 0, 1, 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 = 'referee')
                                                                                                  and id_agenda_segment = (select id from agenda_segment where name = 'referee_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'referee_member_autoinfo')
          )
      );


refresh materialized view mv_agenda_definition;
refresh materialized view mv_agenda_definition_property;

INSERT INTO agenda_definition_to_agenda_definition_mapping
    (name, id_agenda_definition, id_agenda_definition_linked)
VALUES ('member_referee-club_locations_club_locations',
        (SELECT id
         FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'locations'),
        (SELECT id
         FROM mv_agenda_definition
         WHERE agenda_name = 'referee'
           AND segment_name = 'referee_main'
           AND attribute_group_name = 'referee_member_autoinfo'
           AND attribute_name = 'locations'));

INSERT INTO sync_has_mapping (id_agenda_to_agenda_sync, id_agenda_definition_to_agenda_definition_mapping)
VALUES ( (SELECT id FROM agenda_to_agenda_sync WHERE name = 'members_has_referee_agenda_sync')
       , (SELECT id
          FROM agenda_definition_to_agenda_definition_mapping
          WHERE name = 'member_referee-club_locations_club_locations'));


--#14556 - Mass KYU assign action
INSERT INTO agenda_button (id_agenda, name, description, icon, class) VALUES
    (
        (SELECT id FROM agenda WHERE name = 'examiner'),
        'MembersAssignKyu',
        'Hromadný zápis KYU',
        'fal fa-uniform-martial-arts',
        'btn-info'
    );

INSERT INTO agenda_action (name, description, class_name, is_via_rabbit) VALUES
    (
        'MembersAssignKyu',
        'Hromadný zápis KYU',
        'MembersAssignKYUAction',
        true
    );

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

----------------------------------------------------
-- 2.6.20.0
-- SQL for this version ends here
----------------------------------------------------

INSERT INTO configuration.configuration_key (name, data_type) VALUES ('accountant_name', 'string');

INSERT INTO configuration.configuration_definition (name, id_configuration_group, id_configuration_key, is_not_null, default_value,
                                                    example, note, is_read_only, sequence_mandatory, priority)
VALUES ('accountant_name', (SELECT id FROM configuration.configuration_group WHERE name = 'bookkeeping'),
        (SELECT id FROM configuration.configuration_key WHERE name = 'accountant_name'), false, null, null, 'E-mail účetní', false, null, 110);

----------------------------------------------------
-- 2.6.20.1
-- SQL for this version ends here
----------------------------------------------------

-- #14395
UPDATE user_group_has_agenda_definition SET is_create = is_write WHERE 1 = 1;
UPDATE user_group_has_agenda_has_segment_definition SET is_create = is_write WHERE 1 = 1;
UPDATE user_group_has_agenda_has_segment_group_definition SET is_create = is_write WHERE 1 = 1;

MERGE INTO user_group_has_agenda_definition d
USING (SELECT base.id_user_group, base.id_agenda_definition, is_can_read AS is_read, base.is_can_write AS is_write
       FROM (SELECT DISTINCT grp.id AS id_user_group, ad.id AS id_agenda_definition, grp.is_can_read, grp.is_can_write
             FROM (SELECT g.ID, ga.is_can_read, ga.is_can_write
                   FROM user_group g
                            JOIN user_group_has_agenda ga ON g.id = ga.id_user_group
                   WHERE g.name LIKE 'Clenove%'
                     AND ga.id_agenda = (SELECT id FROM agenda WHERE name = 'members')) grp,
                  (SELECT id
                   FROM mv_agenda_definition
                   WHERE full_unique_name IN (
                                              'members-members_main-members_main-foreigner',
                                              'members-members_main-members_main-gender',
                                              'members-members_main-members_main-firstname',
                                              'members-members_main-members_main-surname',
                                              'members-members_main-members_main-dt_birthdate',
                                              'members-members_main-members_main-identifier_value'
                       )) ad) base
       WHERE (base.id_user_group, id_agenda_definition) NOT IN (SELECT DISTINCT id_user_group, id_agenda_definition
                                                                FROM user_group_has_agenda_definition
                                                                WHERE id_user_group IN (SELECT ID
                                                                                        FROM user_group
                                                                                        WHERE name LIKE 'Clenove%'))
       UNION
       (SELECT DISTINCT id_user_group, id_agenda_definition, is_read, is_write
        FROM user_group_has_agenda_definition
        WHERE id_user_group IN (SELECT ID
                                FROM user_group
                                WHERE name LIKE 'Clenove%')
          AND id_agenda_definition IN (SELECT id
                                       FROM mv_agenda_definition
                                       WHERE full_unique_name IN (
                                                                  'members-members_main-members_main-foreigner',
                                                                  'members-members_main-members_main-gender',
                                                                  'members-members_main-members_main-firstname',
                                                                  'members-members_main-members_main-surname',
                                                                  'members-members_main-members_main-dt_birthdate',
                                                                  'members-members_main-members_main-identifier_value'
                                           )))) s
ON (d.id_user_group = s.id_user_group AND d.id_agenda_definition = s.id_agenda_definition)
WHEN MATCHED THEN
    UPDATE
    SET is_create = true
      , is_write = false
WHEN NOT MATCHED THEN
    INSERT (id_user_group, id_agenda_definition, is_read, is_write, is_create)
    VALUES (s.id_user_group, s.id_agenda_definition, s.is_read, false, true);

REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_definition;
refresh materialized view mv_user_group_has_agenda_has_segment_definition;
refresh materialized view mv_user_group_has_agenda_has_segment_group_definition;
refresh materialized view mv_user_group_has_agenda_permission;

----------------------------------------------------
-- 2.6.21.0
-- SQL for this version ends here
----------------------------------------------------

-- fix #15296

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_search_by, max_length
                              , id_agenda_segment_attribute_group_definition
                              , datagrid_priority)
VALUES ( (select id from attribute where name = 'locations')
       , 35, false, NULL, false, true, NULL, 3, 0, 1, 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 = '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')), 40);

refresh materialized view mv_agenda_definition;

INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES ('member_trainer-club_locations_club_locations',
        (SELECT id
         FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'locations'),
        (SELECT id
         FROM mv_agenda_definition
         WHERE agenda_name = 'trainer'
           AND segment_name = 'trainer_main'
           AND attribute_group_name = 'trainer_member_autoinfo'
           AND attribute_name = 'locations'));

INSERT INTO sync_has_mapping (id_agenda_to_agenda_sync, id_agenda_definition_to_agenda_definition_mapping)
VALUES ( (SELECT id FROM agenda_to_agenda_sync WHERE name = 'members_has_trainer_agenda_sync')
       , (SELECT id
          FROM agenda_definition_to_agenda_definition_mapping
          WHERE name = 'member_trainer-club_locations_club_locations'));


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_search_by, max_length
                              , id_agenda_segment_attribute_group_definition, datagrid_priority)
VALUES ( (select id from attribute where name = 'locations')
       , 35, false, NULL, false, true, NULL, 3, 0, 1, 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 = 'examiner')
                                                      and id_agenda_segment =
                                                          (select id from agenda_segment where name = 'examiner_main'))
            and id_agenda_attribute_group =
                (select id from agenda_attribute_group where name = 'examiner_member_autoinfo')), 40);

refresh materialized view mv_agenda_definition;

INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES ('member_examiner-club_locations_club_locations',
        (SELECT id
         FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'locations'),
        (SELECT id
         FROM mv_agenda_definition
         WHERE agenda_name = 'examiner'
           AND segment_name = 'examiner_main'
           AND attribute_group_name = 'examiner_member_autoinfo'
           AND attribute_name = 'locations'));

INSERT INTO sync_has_mapping (id_agenda_to_agenda_sync, id_agenda_definition_to_agenda_definition_mapping)
VALUES ( (SELECT id FROM agenda_to_agenda_sync WHERE name = 'members_has_examiner_agenda_sync')
       , (SELECT id
          FROM agenda_definition_to_agenda_definition_mapping
          WHERE name = 'member_examiner-club_locations_club_locations'));


UPDATE agenda_definition
SET priority = 78
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 = 'locations');

REFRESH MATERIALIZED VIEW mv_agenda_definition;

UPDATE agenda_definition
SET datagrid_priority = 50
WHERE id = (SELECT id
            FROM mv_agenda_definition
            WHERE unique_name = 'referee_main-referee_member_autoinfo-locations');

UPDATE agenda_definition
SET datagrid_priority=78
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 = 'locations');

REFRESH MATERIALIZED VIEW mv_agenda_definition;


----------------------------------------------------
-- 2.6.21.1
-- SQL for this version ends here
----------------------------------------------------


-- #14371
INSERT INTO base.appuser (username, psswd, secret_key_ldap, is_ldap, is_sync_exchange, is_two_factor, fullname,
                          email, id_locale, is_system_user)
VALUES ('judo_gate', NULL, NULL, FALSE, FALSE, FALSE, 'Judo Gate', 'info@evosoft.cz', 1, TRUE);

INSERT INTO flexii.external_application (name, secret, id_appuser) VALUES ('judo_gate', 'EVODUMMYsd46g8as4Afg4gf9AaGaw84bmoaA', (SELECT id FROM base.appuser WHERE username = 'judo_gate'));

INSERT INTO base.user_group (name, is_admin_group, is_superadmin_group, description, is_system_group) VALUES ('judo_gate', false, false, 'Judo Gate', true);

INSERT INTO base.user_group_has_appuser (id_user_group, id_appuser) VALUES ((SELECT id FROM user_group WHERE name = 'judo_gate'), (SELECT id FROM appuser WHERE username = 'judo_gate'));

INSERT INTO flexii.user_group_has_agenda (id_user_group, id_agenda, is_can_read, is_can_write, is_can_create, is_can_delete) VALUES ((SELECT id FROM user_group WHERE name = 'judo_gate'), (SELECT id FROM agenda WHERE name = 'clubs'), true, false, false, false);

refresh materialized view mv_user_group_has_agenda_has_agenda;
refresh materialized view mv_user_group_has_agenda_definition;
refresh materialized view mv_user_group_has_agenda_has_segment_definition;
refresh materialized view mv_user_group_has_agenda_has_segment_group_definition;
refresh materialized view mv_user_group_has_agenda_permission;

INSERT INTO flexii.external_application_api_token (id_external_application, token, dt_valid_from, id_appuser_created_by, id_appuser_updated_by) VALUES ((SELECT id FROM external_application WHERE name = 'judo_gate'), 'EVODUMMY2024AW7RWM9ejE4IrpCM1tCYIg3v', '2024-07-02', 1, 1);

-----------#14371
-- Introducing new 'club application' agenda
INSERT INTO agenda  (name, icon, is_importable) VALUES('application', 'fad fa-file-import', false);

--------------------- MENU THINGS ---------------------------
INSERT INTO menu_link (name, presenter, action, parameters) VALUES('application_default', 'Agenda:Agenda', 'default', '{"name":"application"}');
INSERT INTO menu_link (name, presenter, action, parameters) VALUES('application_detail', 'Agenda:Agenda', 'detail', '{"name":"application"}');

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 = 'application')
      , 'application', 'fad fa-file-import'
      , (select id from menu_link where name = 'application_default'), 110, false);

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

-----------------POSSIBLE APPLICATION STATES--------------
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES('lookup_application_state', 'created', 'Založeno');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES('lookup_application_state', 'approved', 'Schváleno');
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION) VALUES('lookup_application_state', 'rejected', 'Zamítnuto');

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('application_state', 'Stav přihlášky', 0, 'lookup_application_state', NULL, 1, (select id from attribute_data_type where name = 'lookup'));

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 ( 'application_has_club'
       , (select id from agenda where name = 'clubs')
       , (select id from agenda where name = 'application')
       , 1, false, 10, false, false, false, false, false);

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

-------------------------------------------------------------------------------

--basic application info
--- Segment - APPLICATION MAIN
INSERT INTO agenda_segment  ("name", source_table, file_source_table)
VALUES                      ('application_main', 'agenda_values', 'agenda_has_file');
--- Link segment to agenda
INSERT INTO agenda_has_segment_definition (id_agenda, id_agenda_segment, priority, is_main)
VALUES(
          (select id from agenda where name = 'application')
      , (select id from agenda_segment where name = 'application_main')
      , 1, true
      );
--- Segment APPLICATION MAIN has two groups - MAIN + ADDRESS
INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('application_main', false, NULL);
INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('application_address', false, NULL);
--- Link APPLICATION MAIN to the two groups
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 = 'application')
           and id_agenda_segment = (select id from agenda_segment where name = 'application_main')
        )
    , (select id from agenda_attribute_group where name = 'application_main')
    , false
    , 1
    );
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 = 'application')
           and id_agenda_segment = (select id from agenda_segment where name = 'application_main')
        )
    , (select id from agenda_attribute_group where name = 'application_address')
    , false
    , 2
    );


--- Segment - APPLICATION PARENTS
INSERT INTO agenda_segment  ("name", source_table, file_source_table)
VALUES                      ('application_parents', 'agenda_values', 'agenda_has_file');
--- Segment is divided into groups PARENT1, PARENT 2
INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('application_parent1_main', false, NULL);

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

-- Link agenda to segment
INSERT INTO agenda_has_segment_definition (id_agenda, id_agenda_segment, priority, is_main)
VALUES(
          (select id from agenda where name = 'application')
      , (select id from agenda_segment where name = 'application_parents')
      , 2, false
      );
-- Link segment to groups
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 = 'application')
           and id_agenda_segment = (select id from agenda_segment where name = 'application_parents')
        )
    , (select id from agenda_attribute_group where name = 'application_parent1_main')
    , false
    , 1
    );
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 = 'application')
           and id_agenda_segment = (select id from agenda_segment where name = 'application_parents')
        )
    , (select id from agenda_attribute_group where name = 'application_parent2_main')
    , false
    , 2
    );

-------------------------------------------------- END INIT ------------------------------------------------
---------------------------- START DEFINITIONS -----------------------------------------

-------- APPLICATION MAIN ---------------

WITH inserted_attributes AS (
    SELECT * FROM (
                      VALUES
                          ('firstname',           10, true, null, false, 20,      20, false),
                          ('surname',             20, true, null, false, 10,      10, false),
                          ('gender',              30, true, null, false, null,    30, false),
                          ('dt_birthdate',        40, true, null, false, null,    40, false),
                          ('foreigner',           50, true, 0, false, null,    50, false),
                          ('identifier_value',    60, true, null, false, null,    null, false),
                          ('alternate_identifier',70, true, null, false, null,    null, false),
                          ('email',               80, true, null, false, null,    null, false),
                          ('telephone',           90, true, null, false, null,    null, false),
                          ('application_state',   100, false, (select id from master_lookup WHERE lookup_name='lookup_application_state' AND name='created'), true, null,    60, false),
                          ('application_has_club',110, true, null, false, null,   60, false),
                          ('country',             120, true, (select id from lookup_country where alpha2='CZ'), false, null, 80, false),
                          ('street',              130, true, null, false, null,   null, false),
                          ('street_number',       140, true, null, false, null,   null, false),
                          ('house_number',        150, false, null, false, null,  null, false),
                          ('city',                160, true, null, false, null,   70, false),
                          ('postal_code',         170, true, null, false, null,   null, false),
                          ('address_region',      180, true, null, false, null,   null, false),
                          ('nonage',              500, false, null, true, null,   null, true)
                  ) AS inserted_attributes
                      (name, priority, is_not_null, default_value, is_readonly, in_title_and_priority, datagrid_priority, is_hidden))
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_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition,
    datagrid_priority, is_hidden)
SELECT
    (select id from attribute where attribute.name=inserted_attributes.name),
    inserted_attributes.priority,
    inserted_attributes.is_not_null,
    inserted_attributes.default_value,
    false,
    inserted_attributes.is_readonly,
    null,
    3,
    0,
    1,
    false,
    inserted_attributes.in_title_and_priority,
    (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 = 'application')
               and id_agenda_segment = (select id from agenda_segment where name = 'application_main')
         )
       and
             id_agenda_attribute_group = (
             CASE
                 WHEN inserted_attributes.name IN ('country', 'street', 'street_number', 'house_number', 'city', 'postal_code', 'address_region')
                     THEN (select id from agenda_attribute_group where name ='application_address')
                 ELSE (select id from agenda_attribute_group where name ='application_main')
                 END
             )
    ),
    inserted_attributes.datagrid_priority,
    inserted_attributes.is_hidden
FROM inserted_attributes;

------------------------------------------------------------RODIC 1-----------------------------------------
WITH inserted_attributes AS (
    SELECT * FROM (
                      VALUES
                          ('firstname',           200, false, null, false, null,    null),
                          ('surname',             210, false, null, false, null,    null),
                          ('email',               220, false, null, false, null,    null),
                          ('telephone',           230, false, null, false, null,    null),
                          ('country',             240, false, (select id from lookup_country where alpha2='CZ'), false, null,   null),
                          ('street',              250, false, null, false, null,   null),
                          ('street_number',       260, false, null, false, null,   null),
                          ('house_number',        270, false, null, false, null,   null),
                          ('city',                280, false, null, false, null,   null),
                          ('postal_code',         290, false, null, false, null,   null),
                          ('address_region',      300, false, null, false, null,   null)
                  ) AS inserted_attributes
                      (name, priority, is_not_null, default_value, is_readonly, in_title_and_priority, datagrid_priority))
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_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition,
    datagrid_priority)
SELECT
    (select id from attribute where attribute.name=inserted_attributes.name),
    inserted_attributes.priority,
    inserted_attributes.is_not_null,
    inserted_attributes.default_value,
    false,
    inserted_attributes.is_readonly,
    null,
    3,
    0,
    1,
    false,
    inserted_attributes.in_title_and_priority::smallint, --i dont know why it's angry but this helps
    (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 = 'application')
           and id_agenda_segment = (select id from agenda_segment where name = 'application_parents')
     )
       and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'application_parent1_main')
    ),
    inserted_attributes.datagrid_priority::smallint --i dont know why it's angry but this helps
FROM inserted_attributes;

------------------------------------RODIC 2-----------------------------------------------
WITH inserted_attributes AS (
    SELECT * FROM (
                      VALUES
                          ('firstname',           400, false, null, false, null,    null),
                          ('surname',             410, false, null, false, null,    null),
                          ('email',               420, false, null, false, null,    null),
                          ('telephone',           430, false, null, false, null,    null),
                          ('country',             440, false, (select id from lookup_country where alpha2='CZ'), false, null,   null),
                          ('street',              450, false, null, false, null,   null),
                          ('street_number',       460, false, null, false, null,   null),
                          ('house_number',        470, false, null, false, null,   null),
                          ('city',                480, false, null, false, null,   null),
                          ('postal_code',         490, false, null, false, null,   null),
                          ('address_region',      500, false, null, false, null,   null)
                  ) AS inserted_attributes
                      (name, priority, is_not_null, default_value, is_readonly, in_title_and_priority, datagrid_priority))
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_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition,
    datagrid_priority)
SELECT
    (select id from attribute where attribute.name=inserted_attributes.name),
    inserted_attributes.priority,
    inserted_attributes.is_not_null,
    inserted_attributes.default_value,
    false,
    inserted_attributes.is_readonly,
    null,
    3,
    0,
    1,
    false,
    inserted_attributes.in_title_and_priority::smallint, --see above
    (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 = 'application')
           and id_agenda_segment = (select id from agenda_segment where name = 'application_parents')
     )
       and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'application_parent2_main')
    ),
    inserted_attributes.datagrid_priority::smallint --see above
FROM inserted_attributes;

-----------AVATAR
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 = 'application')
             and id_agenda_segment = (select id from agenda_segment where name = 'application_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_search_by, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'avatar')
      , 10, false, NULL, false, false, NULL, 3, 0, 1, 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 = 'application')
                                                                                                  and id_agenda_segment = (select id from agenda_segment where name = 'application_main')
         )
           and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'avatar_group')
          )
      );

REFRESH MATERIALIZED VIEW mv_agenda_definition;
----------------------- DEFINITIONS DONE

--Show appropriate columns in aha view (from club page)
WITH aha_display_attributes AS (
    SELECT * FROM (
        VALUES
            ('email', 10),
            ('dt_birthdate', 20),
            ('application_state', 30)
    ) AS aha_display_attributes (name, column_priority)
)
INSERT INTO agenda_has_agenda_show_attribute
    (id_agenda_has_agenda, id_agenda_definition, datagrid_order_priority, datagrid_order_by, datagrid_column_priority)
SELECT
    (select ID from agenda_has_agenda where name='application_has_club'),
    (select ID from mv_agenda_definition where agenda_name='application' AND segment_name='application_main'
        AND attribute_name=aha_display_attributes.name
     ),
    null,
    null,
    aha_display_attributes.column_priority
FROM aha_display_attributes;

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

--IF is foreigner, rodne cislo not required
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 attribute_name='identifier_value' AND agenda_name='application' AND segment_name='application_main'),
           (SELECT id FROM mv_agenda_definition WHERE attribute_name='foreigner' AND agenda_name='application' AND segment_name='application_main'),
           1, true,
           false, NULL, false, false
       );

----IF is foreigner, alternate identifier is required
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 attribute_name='alternate_identifier' AND agenda_name='application' AND segment_name='application_main'),
           (SELECT id FROM mv_agenda_definition WHERE attribute_name='foreigner' AND agenda_name='application' AND segment_name='application_main'),
           1, false,
           true, NULL, false, false
       );

--IF not foreigner, require rodne cislo
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 attribute_name='identifier_value' AND agenda_name='application' AND segment_name='application_main'),
           (SELECT id FROM mv_agenda_definition WHERE attribute_name='foreigner' AND agenda_name='application' AND segment_name='application_main'),
           0, false, --dont hide if value=0
           true, NULL, false, false
       );

-----------validace rodneho cisla
INSERT INTO flexii.agenda_definition_validation (id_agenda_definition, class_name, priority)
VALUES ((SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'application-application_main-application_main-identifier_value'), 'RcValidation', 10);

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 attribute_name='identifier_value' AND agenda_name='application' AND segment_name='application_main'),
           (SELECT id FROM mv_agenda_definition WHERE attribute_name='foreigner' AND agenda_name='application' AND segment_name='application_main'),
           1, true,
           false, NULL, false, false
       );

-------------------------------nastaveni povinneho zakonneho zastupce pokud dite
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)
SELECT
    id AS id_agenda_definition,
    (SELECT id FROM mv_agenda_definition
          WHERE agenda_name = 'application'
            AND segment_name = 'application_main'
            AND attribute_group_name = 'application_main'
            AND attribute_name = 'nonage'
      ) AS id_agenda_definition_control,
      1 AS value_control,
      false AS is_hidden,
      true AS is_not_null,
      null AS default_value,
      false AS is_disabled,
      false AS is_read_only
FROM mv_agenda_definition WHERE agenda_name = 'application'
              AND segment_name = 'application_parents'
              AND attribute_group_name = 'application_parent1_main'
              AND attribute_name IN ('firstname', 'surname', 'email', 'telephone');

------------- nastavení default filtru
INSERT INTO agenda_datagrid_default_filter (id_agenda_definition, value)
VALUES (
           (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'application-application_main-application_main-application_state'),
           (SELECT id FROM master_lookup WHERE lookup_name = 'lookup_application_state' AND name = 'created')
       );

--------oprávnění pro čtení všech přihlášek
----create new user group for dudes who can edit + approve prihlasky from their club
INSERT INTO base.user_group (description, name) VALUES
    ('Klubové přihlášky - vše - editace + schválení', 'Prihlasky_Edit_Approve');

----allow this new right access to applications agenda
INSERT INTO user_group_has_agenda (id_user_group, id_agenda,
                                   is_can_read, is_can_write, is_can_create, is_can_delete)
VALUES
    ((SELECT id FROM base.user_group WHERE name = 'Prihlasky_Edit_Approve'),
     (SELECT id FROM agenda WHERE name = 'application'),
     TRUE, TRUE, TRUE, TRUE);

--------oprávnění klubových adminů
----create new user group for dudes who can edit + approve prihlasky from their club
INSERT INTO base.user_group (description, name) VALUES
    ('Klubové přihlášky - přihlášky klubu admina - editace + schválení', 'PrihlaskyKlubu_Edit_Approve');

----allow this new right access to applications agenda
INSERT INTO user_group_has_agenda (id_user_group, id_agenda,
                                   is_can_read, is_can_write, is_can_create, is_can_delete)
VALUES
    ((SELECT id FROM base.user_group WHERE name = 'PrihlaskyKlubu_Edit_Approve'),
     (SELECT id FROM agenda WHERE name = 'application'),
     TRUE, TRUE, TRUE, TRUE);

---but can only see applications from admins club

INSERT INTO user_group_has_definition_value_for_aha
(name, id_recursion, id_user_group, id_agenda, id_agenda_has_agenda, id_agenda_definition, value)
VALUES
    ('application_has_club_and_admin_lvl1',
     null,
     (SELECT id FROM base.user_group WHERE name = 'PrihlaskyKlubu_Edit_Approve'),
     (SELECT id FROM agenda WHERE name = 'application'),
     (SELECT id FROM agenda_has_agenda WHERE name = 'application_has_club'),
     null, null)
;

INSERT INTO user_group_has_definition_value_for_aha
(name, id_recursion, id_user_group, id_agenda, id_agenda_has_agenda, id_agenda_definition, value)
VALUES
    ('application_has_club_and_admin_lvl2',
     (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'application_has_club_and_admin_lvl1'),
     (SELECT id FROM base.user_group WHERE name = 'PrihlaskyKlubu_Edit_Approve'),
     null,
     (SELECT id FROM agenda_has_agenda WHERE name = 'club_has_administrator'),
     (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'club_administrator' AND segment_name = 'club_administrator_main' AND attribute_group_name = 'club_administrator_main' AND attribute_name = 'appuser'),
     null)
;


-------------------------------------------- tlačítka


INSERT INTO agenda_action (name, description, class_name)
VALUES ('ApplicationApproval', 'Schválit', 'ApplicationApprovalAction');

INSERT INTO agenda_button (id_agenda, name, description, icon, class)
VALUES ((SELECT id FROM agenda WHERE name = 'application'), 'ApplicationApproval', 'Schválit', 'fal fa-check', 'btn-success');

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



INSERT INTO agenda_action (name, description, class_name)
VALUES ('ApplicationReject', 'Zamítnout', 'ApplicationRejectAction');

INSERT INTO agenda_button (id_agenda, name, description, icon, class)
VALUES ((SELECT id FROM agenda WHERE name = 'application'), 'ApplicationReject', 'Zamítnout', 'fal fa-cancel', 'btn-danger remove-btn');

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

INSERT INTO flexii.user_group_has_agenda (id_user_group, id_agenda, is_can_read, is_can_write, is_can_create, is_can_delete) VALUES ((SELECT id FROM user_group WHERE name = 'judo_gate'), (SELECT id FROM agenda WHERE name = 'application'), false, true, true, false);
INSERT INTO flexii.user_group_has_agenda_has_segment_definition (id_user_group, id_agenda_has_segment_definition, is_read, is_write)
VALUES (
        (SELECT id FROM user_group WHERE name = 'judo_gate'),
        (SELECT id FROM agenda_has_segment_definition WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'application') AND id_agenda_segment = (SELECT id FROM agenda_segment WHERE name = 'application_main')),
        true,
        true
       );

UPDATE agenda_definition SET is_read_only = true, is_disabled = false WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'application-application_main-application_main-application_has_club');

REFRESH MATERIALIZED VIEW mv_agenda_definition;
REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_definition;
REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_has_segment_definition;
REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_has_segment_group_definition;
REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_permission;
REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_has_agenda;
REFRESH MATERIALIZED VIEW mv_agenda_definition_property;
REFRESH MATERIALIZED VIEW mv_agenda_has_agenda_definition_property;

--

--#14171
--Reset all existing values of active_fighter attribute
UPDATE agenda_values
SET value = (
    SELECT id FROM master_lookup
    WHERE lookup_name='lookup_active_fighter'
      AND name='stat0'
)
WHERE id_agenda_definition = (
    SELECT id FROM mv_agenda_definition
    WHERE full_unique_name='members-members_main-members_main-active_fighter'
)
;

--Delete unused values
UPDATE master_lookup SET enabled=false
WHERE lookup_name='lookup_active_fighter'
  AND name IN ('stat2', 'stat3');

--Set attribute as disabled && not necessary
UPDATE agenda_definition
SET is_disabled=true, is_not_null=false
WHERE id = (
    SELECT id FROM mv_agenda_definition
    WHERE full_unique_name = 'members-members_main-members_main-active_fighter'
);

create or replace function __is_date(s varchar) returns boolean as $$
begin
    perform s::date;
    return true;
exception when others then
    return false;
end;
$$ language plpgsql;

----------------------------------------------------
-- 2.6.22.0
-- SQL for this version ends here
----------------------------------------------------

-- user group is not relevant anymore
UPDATE base.user_group SET enabled = false WHERE name = 'ClenskePoplatky_Vse_EditaceFaktury';

refresh materialized view mv_user_group_has_agenda_has_agenda;
refresh materialized view mv_user_group_has_agenda_definition;
refresh materialized view mv_user_group_has_agenda_has_segment_definition;
refresh materialized view mv_user_group_has_agenda_has_segment_group_definition;
refresh materialized view mv_user_group_has_agenda_permission;

----------------------------------------------------
-- 2.6.22.1
-- SQL for this version ends here
----------------------------------------------------

UPDATE flexii.agenda_definition
SET enabled = FALSE
WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'application-application_main-application_main-nonage');

refresh materialized view mv_agenda_definition;

UPDATE flexii.external_application SET is_encrypted = true WHERE name = 'judo_gate';

----------------------------------------------------
-- 2.6.22.2
-- SQL for this version ends here
----------------------------------------------------

UPDATE flexii.agenda_button SET confirmation = 'Opravdu chcete přihlášku schválit? Akci není možné vrátit.' WHERE name = 'ApplicationApproval';
UPDATE flexii.agenda_button SET confirmation = 'Opravdu chcete přihlášku odmítnout? Akci není možné vrátit.' WHERE name = 'ApplicationReject';

-- remove slash from member personal identifier - agenda members
UPDATE agenda_values SET value = REPLACE(value, '/', '') WHERE id_agenda_definition = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-identifier_value');

-- remove slash from member personal identifier - agenda application
UPDATE agenda_values SET value = REPLACE(value, '/', '') WHERE id_agenda_definition = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'application-application_main-application_main-identifier_value');

INSERT INTO flexii.agenda_definition_validation (id_agenda_definition, class_name, priority)
VALUES ((SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'application-application_main-application_main-identifier_value'), 'WithoutSlashValidation', 5);

UPDATE agenda set is_creatable = false where name = 'application';


-- #15328 - added permission group to all club_admin
INSERT INTO user_group_has_appuser (id_user_group, id_appuser)
SELECT id_user_group, id_appuser FROM (
  SELECT DISTINCT VALUE::INT AS id_appuser
      FROM agenda_values
      WHERE id_agenda_definition = (
            SELECT id FROM mv_agenda_definition
               WHERE full_unique_name = 'club_administrator-club_administrator_main-club_administrator_main-appuser'
        )
        AND VALUE::INT IN (
            SELECT id_appuser
               FROM user_group_has_appuser
               WHERE id_user_group = (SELECT id FROM user_group WHERE NAME = 'Kluby_AdminemKlubu_EditLimitovan+ObjednaniPoplatku+TiskKaret')
                 AND ENABLED = TRUE
        )
) usr,
(SELECT id AS id_user_group FROM user_group WHERE name = 'PrihlaskyKlubu_Edit_Approve') grp;

refresh materialized view mv_user_group_has_agenda_has_agenda;
refresh materialized view mv_user_group_has_agenda_definition;
refresh materialized view mv_user_group_has_agenda_has_segment_definition;
refresh materialized view mv_user_group_has_agenda_has_segment_group_definition;
refresh materialized view mv_user_group_has_agenda_permission;

----------------------------------------------------
-- 2.6.22.3
-- SQL for this version ends here
----------------------------------------------------

UPDATE agenda set is_creatable = true where name = 'application';

update user_group_has_agenda set is_can_create = false
    where id_user_group in (
        select id from user_group where name in ('PrihlaskyKlubu_Edit_Approve', 'Prihlasky_Edit_Approve')
    );

--#15345 Add club admin access to judo_gate
INSERT INTO flexii.user_group_has_agenda
    (id_user_group,
     id_agenda,
     is_can_read, is_can_write, is_can_create, is_can_delete)
VALUES (
        (SELECT id FROM base.user_group WHERE name = 'judo_gate'),
        (SELECT id FROM agenda WHERE name = 'club_administrator'),
        true, false, false, false
       );

INSERT INTO flexii.user_group_has_agenda_has_agenda
    (id_user_group,
     id_agenda_has_agenda,
     is_can_read, is_can_write, is_can_create, is_can_delete)
VALUES (
        (SELECT id FROM base.user_group WHERE name = 'judo_gate'),
        (SELECT id FROM agenda_has_agenda WHERE name = 'club_has_administrator'),
        true, false, false, false
       );

refresh materialized view mv_user_group_has_agenda_has_agenda;
refresh materialized view mv_user_group_has_agenda_definition;
refresh materialized view mv_user_group_has_agenda_has_segment_definition;
refresh materialized view mv_user_group_has_agenda_has_segment_group_definition;
refresh materialized view mv_user_group_has_agenda_permission;

----------------------------------------------------
-- 2.6.22.4
-- SQL for this version ends here
----------------------------------------------------

--Fix duplicate row (caused by typo)
UPDATE agenda_definition_specification_by_value
SET     value_control = 0, --Should have been <0> (i.e. NOT FOREIGNER)
        id_agenda_definition = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'application-application_main-application_main-alternate_identifier')
--Should have been <alternate identifier> (which should be shown ONLY to foreigners)
WHERE id = (SELECT id FROM agenda_definition_specification_by_value WHERE id_agenda_definition       = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'application-application_main-application_main-identifier_value')
                                                                      AND id_agenda_definition_control = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'application-application_main-application_main-foreigner')
                                                                      AND value_control::int = 1
                                                                      AND is_hidden = true        -- is hidden   (because czech citizens don't care about alt. identifiers)
                                                                      AND is_not_null = false     -- is nullable (because czech citizens don't require alt. identifiers)
            LIMIT 1 -- because there are two duplicate rows; only fix one
);

----------------------------------------------------
-- 2.6.22.5
-- SQL for this version ends here
----------------------------------------------------

--#14827
--Allow club admins to edit GPS x/y
INSERT INTO user_group_has_agenda_definition(id_user_group, id_agenda_definition) SELECT
                                                                                      (SELECT id FROM base.user_group WHERE name='Kluby_AdminemKlubu_EditLimitovan+ObjednaniPoplatku+TiskKaret'),
                                                                                      mad.id
FROM mv_agenda_definition mad
WHERE mad.unique_name IN ('clubs_main-clubs_main-club_gps_x', 'clubs_main-clubs_main-club_gps_y');

--GPS x/y no longer readonly
UPDATE agenda_definition SET is_read_only=false
WHERE id IN (SELECT id FROM mv_agenda_definition
             WHERE unique_name IN ('clubs_main-clubs_main-club_gps_x', 'clubs_main-clubs_main-club_gps_y')
);
REFRESH MATERIALIZED VIEW mv_agenda_definition;

--Add GPS validation
INSERT INTO flexii.agenda_definition_validation (id_agenda_definition, class_name, priority)
SELECT mad.id, 'GPSValidation', 10
FROM mv_agenda_definition mad
WHERE mad.unique_name IN ('clubs_main-clubs_main-club_gps_x', 'clubs_main-clubs_main-club_gps_y');

----------------------------------------------------
-- 2.6.23.0
-- SQL for this version ends here
----------------------------------------------------

-- #15426
UPDATE flexii.agenda_definition SET is_disabled = true WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'referee-instructor_main-instructor_main-dt_validity_to');
REFRESH MATERIALIZED VIEW mv_agenda_definition;

----------------------------------------------------
-- 2.6.23.1
-- SQL for this version ends here
----------------------------------------------------

--Add GPS validation for sports fields
INSERT INTO flexii.agenda_definition_validation (id_agenda_definition, class_name, priority)
SELECT mad.id, 'GPSValidation', 10
FROM mv_agenda_definition mad
WHERE mad.full_unique_name IN ('clubs-club_sport_fields-club_sport_fields_main-club_gps_x', 'clubs-club_sport_fields-club_sport_fields_main-club_gps_y');

--Fix GPS order
UPDATE flexii.agenda_definition ad
SET priority=priority-10
WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name='clubs-clubs_main-clubs_main-club_gps_x');
REFRESH MATERIALIZED VIEW mv_agenda_definition;

----------------------------------------------------
-- 2.6.23.2
-- SQL for this version ends here
----------------------------------------------------

-- #15374
INSERT INTO agenda_has_agenda_show_attribute (id_agenda_has_agenda, id_agenda_definition, is_hidden_in_datagrid, is_only_for_export,
                                              datagrid_column_priority)
VALUES ((SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_members'),
        (SELECT id
         FROM mv_agenda_definition
         WHERE full_unique_name = 'members-members_main-members_main-id_member'),
        false,
        false,
        2
       );


-- #15255
UPDATE flexii.agenda_has_agenda
SET is_search_limited_by_permission_linked = FALSE, is_deletable_linked = true
WHERE name = 'tournaments_has_tournament_administrator';

UPDATE flexii.user_group_has_agenda_has_agenda
SET is_can_write = true, is_can_create = true, is_can_delete = true
WHERE id_user_group = (SELECT id FROM user_group WHERE name = 'Turnaje_AdminemTurnaje_Edit+Spusteni+TiskHesel') AND
id_agenda_has_agenda = (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_tournament_administrator');

REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_has_agenda;

INSERT INTO flexii.user_group_has_agenda (id_user_group, id_agenda, is_can_read, is_can_write, is_can_create, is_can_delete)
VALUES (
           (SELECT id FROM user_group WHERE name = 'Turnaje_AdminemTurnaje_Edit+Spusteni+TiskHesel'),
           (SELECT id FROM agenda WHERE name = 'tournament_administrator'),
        true,
        false,
        false,
        false
       );

refresh materialized view mv_user_group_has_agenda_definition;
refresh materialized view mv_user_group_has_agenda_has_segment_definition;
refresh materialized view mv_user_group_has_agenda_has_segment_group_definition;
refresh materialized view mv_user_group_has_agenda_permission;


--#14828
INSERT INTO flexii.custom_import_type (id_agenda, name, description) VALUES ((SELECT id FROM agenda WHERE name = 'tournaments'), 'MembersToTournamentImport', 'Import členů do turnaje');

--Add new column to custom_import (in this case, for storing info about which club is the source)
ALTER TABLE custom_import
    ADD COLUMN custom_data json DEFAULT null;

--Attribute for allowing clubs to import members to tournaments
INSERT INTO attribute
(name, description, data_type_precision,
 lookup, lookup_columns, weight,
 id_attribute_data_type)
VALUES
    ('allow_tournament_import', 'Povolit hromadné přihlášení na turnaj', 0,
     'lookup_yes_no', null, 15,
     (select id from attribute_data_type where name = 'lookup'))
;

--Add it to club agenda
INSERT INTO agenda_definition
(id_attribute, priority, is_not_null, in_title_and_priority,
 datagrid_order_priority, min_length, max_length,
 min_value, max_value, id_agenda_definition_depended,
 id_agenda_segment_attribute_group_definition, datagrid_priority,
 default_value)
VALUES
    (
        (select id from attribute where name = 'allow_tournament_import'), 190, true, null,
        null, null, null,
        null, null, 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_main')
                     and id_agenda_attribute_group =
                         (select id from agenda_attribute_group where name = 'clubs_main'))
        ), null,
        (SELECT id FROM master_lookup WHERE lookup_name='lookup_yes_no' AND name='no')
    )
;

--#15256
INSERT INTO agenda_has_agenda
(name, id_agenda, id_agenda_linked, is_unique, priority,
 id_agenda_definition_max_count_limit, uid_datagrid_order_priority,
 uid_datagrid_order_by, name_datagrid_order_priority, name_datagrid_order_by,
 relation,
 is_addable, is_deletable, is_editable_agenda, is_editable_agenda_linked, is_deletable_linked,
 is_custom_name,
 is_quick_add_link, is_quick_add_link_linked, is_quick_add_agenda, is_quick_add_agenda_linked, is_addable_linked)
VALUES (
           'tournaments_has_past_members',
           (SELECT id FROM agenda WHERE name='members'),
           (SELECT id FROM agenda WHERE name='tournaments'),
           false,
           151,
           null,
           null, null, null, null,
           4, --this means N:M
           false, false, false, false, false,  --neither addable nor deletable
           true,
           false, false, false, false, false --cant quick add anywhere
       );

INSERT INTO attribute
(name, description, lookup_columns, id_attribute_data_type, custom_data_class)
VALUES (
           'matches_won',
           'Počet vyhraných zápasů',
           null,
           (SELECT id FROM attribute_data_type WHERE name='int'),
           null
       );

INSERT INTO attribute
(name, description, lookup_columns, id_attribute_data_type, custom_data_class)
VALUES (
           'category_ranking',
           'Pořadí v kategorii',
           null,
           (SELECT id FROM attribute_data_type WHERE name='int'),
           null
       );

INSERT INTO attribute (name, description, data_type_precision,
                       lookup, lookup_columns, weight,
                       id_attribute_data_type)
VALUES (
           'is_hajime',
           'Řízeno přes Hajime',
           0,
           'lookup_yes_no',
           null, 150,
           (select id from attribute_data_type where name = 'lookup')
       );

INSERT INTO agenda_definition
    (id_attribute, priority, in_title_and_priority,
     datagrid_order_priority, min_length, max_length,
     min_value, max_value, id_agenda_definition_depended,
     id_agenda_segment_attribute_group_definition, datagrid_priority)
VALUES (
           (select id from attribute WHERE name='is_hajime'),
           160, null, null, null, null, null, null, null,
           (SELECT id_agenda_segment_attribute_group_definition
            FROM mv_agenda_definition
            WHERE attribute_group_name='tournaments_main' AND segment_name='tournaments_main'
            LIMIT 1),
           null
       );

INSERT INTO agenda_has_agenda_definition
    (id_agenda_has_agenda, id_attribute, id_agenda_primary, default_value,
     id_agenda_has_agenda_dependent, datagrid_order_priority, datagrid_order_by, datagrid_column_priority)
VALUES (
           (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_past_members'),
           (SELECT id FROM attribute WHERE name = 'category_ranking'),
           (SELECT id FROM agenda WHERE name = 'tournaments'),
           null,
           null,
           null, null, 11
       );

INSERT INTO agenda_has_agenda_definition
(id_agenda_has_agenda, id_attribute, id_agenda_primary, default_value,
 id_agenda_has_agenda_dependent, datagrid_order_priority, datagrid_order_by, datagrid_column_priority)
VALUES (
           (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_past_members'),
           (SELECT id FROM attribute WHERE name = 'matches_won'),
           (SELECT id FROM agenda WHERE name = 'tournaments'),
           null,
           null,
           null, null, 10
       );

INSERT INTO agenda_has_agenda_definition
(id_agenda_has_agenda, id_attribute, id_agenda_primary, default_value,
 id_agenda_has_agenda_dependent, datagrid_order_priority, datagrid_order_by, datagrid_column_priority)
VALUES (
           (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_past_members'),
           (SELECT id FROM attribute WHERE name = 'weight_category'),
           (SELECT id FROM agenda WHERE name = 'tournaments'),
           null,
           null,
           null, null, 9
       );

INSERT INTO agenda_has_agenda_definition
(id_agenda_has_agenda, id_attribute, id_agenda_primary, default_value,
 id_agenda_has_agenda_dependent, datagrid_order_priority, datagrid_order_by, datagrid_column_priority)
VALUES (
           (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_past_members'),
           (SELECT id FROM attribute WHERE name = 'age_category'),
           (SELECT id FROM agenda WHERE name = 'tournaments'),
           null,
           null,
           null, null, 8
       );

INSERT INTO custom_import_type (id_agenda, name, description)
VALUES (
   (SELECT id FROM agenda WHERE name='tournaments'),
   'PastMembersToTournamentImport',
   'Import závodníků turnaje'
);

REFRESH MATERIALIZED VIEW mv_agenda_has_agenda_definition_property;

UPDATE agenda_definition
SET default_value = (SELECT id FROM master_lookup WHERE lookup_name = 'lookup_yes_no' AND name = 'no'),
    is_not_null   = TRUE
WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'tournaments-tournaments_main-tournaments_main-is_hajime');

REFRESH MATERIALIZED VIEW mv_agenda_definition;

----------------------------------------------------
-- 2.6.24.0
-- SQL for this version ends here
----------------------------------------------------

REFRESH MATERIALIZED VIEW mv_agenda_definition_property;
REFRESH MATERIALIZED VIEW mv_agenda_has_agenda_definition_property;

UPDATE agenda_definition SET tooltip = '<strong>Standardní bez omezení</strong><ul><li>nepovinné kategorie při přihlášení člena</li><li>v Hajime nenavážení závodníci nespadají do rozlosování</li></ul><strong>Standardní s povinnou hmotnostní kategorií</strong> <ul><li>povinné kategorie při přihlášení člena</li><li>v Hajime nenavážení závodníci spadají do rozlosování</li></ul></p>' WHERE id =  (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'tournaments-tournaments_main-tournaments_main-tournament_type');
REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- #15522
INSERT INTO user_group_has_agenda_has_agenda (id_user_group, id_agenda_has_agenda, is_can_read, is_can_write, is_can_create, is_can_delete)
SELECT grp.id, aha.id, TRUE, true, true, true
FROM (SELECT id
      FROM user_group
      WHERE name IN (
                     'Turnaje_Vse_Edit+Pridani',
                     'Turnaje_AdminemTurnaje_Edit+Spusteni+TiskHesel',
                     'Turnaje_TurnajAktivniAPoradanKlubemAdmina_Cteni+PrihlaseniClenuSvehoKlubu',
                     'Turnaje_JihoceskyKraj_Edit+Pridani',
                     'Turnaje_JihomoravskyKraj_Edit+Pridani',
                     'Turnaje_KarlovarskyKraj_Edit+Pridani',
                     'Turnaje_KralovehradeckyKraj_Edit+Pridani',
                     'Turnaje_LibereckyKraj_Edit+Pridani',
                     'Turnaje_MoravskoslezskyKraj_Edit+Pridani',
                     'Turnaje_OlomouckyKraj_Edit+Pridani',
                     'Turnaje_PardubickyKraj_Edit+Pridani',
                     'Turnaje_PlzenskyKraj_Edit+Pridani',
                     'Turnaje_Praha_Edit+Pridani',
                     'Turnaje_StredoceskyKraj_Edit+Pridani',
                     'Turnaje_UsteckyKraj_Edit+Pridani',
                     'Turnaje_Vysocina_Edit+Pridani',
                     'Turnaje_ZlinskyKraj_Edit+Pridani'
          )) grp,
     (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_past_members') aha;

REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_has_agenda;

----------------------------------------------------
-- 2.6.24.1
-- SQL for this version ends here
----------------------------------------------------

-- #15515
UPDATE user_group
SET description = 'Členské příspěvky - vše - čtení', name = 'ClenskePrispevky_Vse_Cteni'
WHERE name = 'ClenskePoplatky_Vse_Cteni';

UPDATE user_group
SET description = 'Členské příspěvky - vše - zaplaceno + zrušit', name = 'ClenskePrispevky_Vse_Zaplaceno+Zrusit'
WHERE name = 'ClenskePoplatky_Vse_Zaplaceno+Zrusit';

UPDATE user_group
SET description = 'Členské příspěvky - vše - editace faktury', name = 'ClenskePrispevky_Vse_EditaceFaktury'
WHERE name = 'ClenskePoplatky_Vse_EditaceFaktury';

-- is_unq fix
UPDATE flexii.agenda_definition
SET is_unique = FALSE
WHERE ID IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN (
             'trainer-trainer_main-trainer_member_autoinfo-id_member_sync',
             'official-official_main-official_member_autoinfo-id_member_sync',
             'examiner-examiner_main-examiner_member_autoinfo-id_member_sync',
             'referee-referee_main-referee_member_autoinfo-id_member_sync',
             'club_administrator-club_administrator_main-club_administrator_autoinfo-id_member_sync',
             'tournament_administrator-tournament_administrator_main-tournament_administrator_member_autoinfo-id_member_sync',
             'referee-referee_main-referee_member_autoinfo-email'
    ));
REFRESH MATERIALIZED VIEW mv_agenda_definition;

----------------------------------------------------
-- 2.6.25.0
-- SQL for this version ends here
----------------------------------------------------

-- #15570
INSERT INTO agenda_definition_custom (id_agenda_definition, class_name) VALUES ((SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-kyu-kyu-agenda_examiner'), 'ExaminerKyu');

----------------------------------------------------
-- 2.6.26.0
-- SQL for this version ends here
----------------------------------------------------

-- #11662
UPDATE flexii.agenda_attribute_group SET name_short = 'avat' WHERE name = 'avatar_group';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'members_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'addrm' WHERE name = 'members_address_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'cntct' WHERE name = 'members_contact_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'fees' WHERE name = 'members_fee';
UPDATE flexii.agenda_attribute_group SET name_short = 'kyu' WHERE name = 'kyu';
UPDATE flexii.agenda_attribute_group SET name_short = 'dan' WHERE name = 'dan';
UPDATE flexii.agenda_attribute_group SET name_short = 'attch' WHERE name = 'member_attachments';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'parents_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'addrm' WHERE name = 'parents_address_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'cont' WHERE name = 'parents_contact_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'clubs_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'addrm' WHERE name = 'clubs_address_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'addrp' WHERE name = 'clubs_address_postal';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'tournaments_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'addrm' WHERE name = 'tournaments_address_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'categ' WHERE name = 'tournament_category_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'info' WHERE name = 'tournament_information_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'propo' WHERE name = 'tournament_proposition_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'competitions_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'gdpr_training_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'attnd' WHERE name = 'gdpr_training_attendee';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'gdpr_incident_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'gdpr_audit_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'gdpr_deletion_request_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'gdpr_portability_request_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'gdpr_objection_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'gdpr_dpo_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'psswd' WHERE name = 'access_passwords';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'tournament_manager_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'docum' WHERE name = 'club_document';
UPDATE flexii.agenda_attribute_group SET name_short = 'sttry' WHERE name = 'club_statutory';
UPDATE flexii.agenda_attribute_group SET name_short = 'hnrre' WHERE name = 'honor_regional';
UPDATE flexii.agenda_attribute_group SET name_short = 'hnrun' WHERE name = 'honor_union';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'tournament_administrator_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'sttes' WHERE name = 'club_statutes_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'direc' WHERE name = 'tournament_director_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'info' WHERE name = 'tournament_administrator_info_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'official_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'role' WHERE name = 'official_role_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'trainer_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'info' WHERE name = 'trainer_member_autoinfo';
UPDATE flexii.agenda_attribute_group SET name_short = 'class' WHERE name = 'trainer_class_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'point' WHERE name = 'credit_point_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'info' WHERE name = 'official_member_autoinfo';
UPDATE flexii.agenda_attribute_group SET name_short = 'minfo' WHERE name = 'tournament_administrator_member_autoinfo';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'examiner_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'info' WHERE name = 'examiner_member_autoinfo';
UPDATE flexii.agenda_attribute_group SET name_short = 'class' WHERE name = 'examiner_class_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'referee_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'info' WHERE name = 'referee_member_autoinfo';
UPDATE flexii.agenda_attribute_group SET name_short = 'class' WHERE name = 'referee_class_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'class' WHERE name = 'international_referee_class_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'instructor_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'club_administrator_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'info' WHERE name = 'club_administrator_autoinfo';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'union_fees_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'refer' WHERE name = 'tournament_referee_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'info' WHERE name = 'union_fees_autoinfo';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'judo_event_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'addr' WHERE name = 'judo_event_address';
UPDATE flexii.agenda_attribute_group SET name_short = 'organ' WHERE name = 'judo_event_organizer';
UPDATE flexii.agenda_attribute_group SET name_short = 'respp' WHERE name = 'judo_event_responsible_person';
UPDATE flexii.agenda_attribute_group SET name_short = 'info' WHERE name = 'judo_event_information';
UPDATE flexii.agenda_attribute_group SET name_short = 'propo' WHERE name = 'judo_event_proposition';
UPDATE flexii.agenda_attribute_group SET name_short = 'field' WHERE name = 'club_sport_fields_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'reslt' WHERE name = 'tournament_results_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'kata' WHERE name = 'referee_kata';
UPDATE flexii.agenda_attribute_group SET name_short = 'main' WHERE name = 'application_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'addr' WHERE name = 'application_address';
UPDATE flexii.agenda_attribute_group SET name_short = 'prnt1' WHERE name = 'application_parent1_main';
UPDATE flexii.agenda_attribute_group SET name_short = 'prnt2' WHERE name = 'application_parent2_main';

UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'members_main';
UPDATE flexii.agenda_segment SET name_short = 'fee' WHERE name = 'members_fee';
UPDATE flexii.agenda_segment SET name_short = 'kyu' WHERE name = 'kyu';
UPDATE flexii.agenda_segment SET name_short = 'dan' WHERE name = 'dan';
UPDATE flexii.agenda_segment SET name_short = 'attch' WHERE name = 'member_attachments';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'parents_main';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'clubs_main';
UPDATE flexii.agenda_segment SET name_short = 'addr' WHERE name = 'clubs_address';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'tournaments_main';
UPDATE flexii.agenda_segment SET name_short = 'categ' WHERE name = 'tournament_category';
UPDATE flexii.agenda_segment SET name_short = 'info' WHERE name = 'tournament_information';
UPDATE flexii.agenda_segment SET name_short = 'propo' WHERE name = 'tournament_proposition';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'competitions_main';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'gdpr_training_main';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'gdpr_incident_main';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'gdpr_audit_main';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'gdpr_deletion_request_main';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'gdpr_portability_request_main';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'gdpr_objection_main';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'gdpr_dpo_main';
UPDATE flexii.agenda_segment SET name_short = 'acces' WHERE name = 'tournament_access';
UPDATE flexii.agenda_segment SET name_short = 'docu' WHERE name = 'club_document';
UPDATE flexii.agenda_segment SET name_short = 'sttry' WHERE name = 'club_statutory';
UPDATE flexii.agenda_segment SET name_short = 'hnrre' WHERE name = 'honor_regional';
UPDATE flexii.agenda_segment SET name_short = 'hnrun' WHERE name = 'honor_union';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'tournament_administrator_main';
UPDATE flexii.agenda_segment SET name_short = 'stst' WHERE name = 'club_statutes';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'official_main';
UPDATE flexii.agenda_segment SET name_short = 'role' WHERE name = 'official_role_main';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'trainer_main';
UPDATE flexii.agenda_segment SET name_short = 'class' WHERE name = 'trainer_class_main';
UPDATE flexii.agenda_segment SET name_short = 'point' WHERE name = 'credit_point_main';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'examiner_main';
UPDATE flexii.agenda_segment SET name_short = 'class' WHERE name = 'examiner_class_main';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'referee_main';
UPDATE flexii.agenda_segment SET name_short = 'class' WHERE name = 'referee_class_main';
UPDATE flexii.agenda_segment SET name_short = 'iclas' WHERE name = 'international_referee_class_main';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'instructor_main';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'club_administrator_main';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'union_fees_main';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'judo_event_main';
UPDATE flexii.agenda_segment SET name_short = 'info' WHERE name = 'judo_event_information';
UPDATE flexii.agenda_segment SET name_short = 'prop' WHERE name = 'judo_event_proposition';
UPDATE flexii.agenda_segment SET name_short = 'field' WHERE name = 'club_sport_fields';
UPDATE flexii.agenda_segment SET name_short = 'rslts' WHERE name = 'tournament_results';
UPDATE flexii.agenda_segment SET name_short = 'kata' WHERE name = 'referee_kata';
UPDATE flexii.agenda_segment SET name_short = 'main' WHERE name = 'application_main';
UPDATE flexii.agenda_segment SET name_short = 'prnts' WHERE name = 'application_parents';

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- #15650
UPDATE flexii.lookup_categories SET description = 'Starší přípravka U10' WHERE name = '2024_u10';
UPDATE flexii.lookup_categories SET description = 'Mladší žačky WU12' WHERE name = '2024_wu12';
UPDATE flexii.lookup_categories SET description = 'Starší žačky WU14' WHERE name = '2024_wu14';

refresh materialized view lookup_judo_categories;

----------------------------------------------------
-- 2.6.27.0
-- SQL for this version ends here
----------------------------------------------------

-- #15569
UPDATE agenda_definition SET is_search_by = true WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('parents-parents_main-parents_contact_main-email', 'parents-parents_main-parents_contact_main-telephone'));
UPDATE agenda_definition SET is_search_by = false WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('parents-parents_main-parents_main-id_parent'));
REFRESH MATERIALIZED VIEW mv_agenda_definition;

INSERT INTO definition_table_value (id_definition_key, id_definition, value)
VALUES ((SELECT id FROM definition_key WHERE table_name = 'agenda_has_agenda' AND name = 'is_create_link_on_attribute'),
        (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_parents'), TRUE);

REFRESH MATERIALIZED VIEW mv_agenda_has_agenda_property;


-- #15568
INSERT INTO user_group_has_agenda (id_user_group, id_agenda, is_can_read, is_can_write, is_can_create, is_can_delete)
VALUES ((SELECT id FROM user_group WHERE name = 'Clenove_ClenoveKlubuAdmina_EditLimitovan+Pridani'),
        (SELECT id FROM agenda WHERE name = 'parents'),
        TRUE, TRUE, TRUE, FALSE);

INSERT INTO user_group_has_definition_value_for_aha (name, id_recursion, id_user_group, id_agenda, id_agenda_has_agenda, id_agenda_definition, value)
VALUES
    ('parent_has_member_has_club_and_admin_lvl1', null, (SELECT id FROM user_group WHERE name = 'Clenove_ClenoveKlubuAdmina_EditLimitovan+Pridani'), (SELECT id FROM agenda WHERE name = 'parents'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_parents'), null, null)
;

INSERT INTO user_group_has_definition_value_for_aha (name, id_recursion, id_user_group, id_agenda, id_agenda_has_agenda, id_agenda_definition, value)
VALUES
    ('parent_has_member_has_club_and_admin_lvl2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'parent_has_member_has_club_and_admin_lvl1'), (SELECT id FROM user_group WHERE name = 'Clenove_ClenoveKlubuAdmina_EditLimitovan+Pridani'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), null, null)
;

INSERT INTO user_group_has_definition_value_for_aha (name, id_recursion, id_user_group, id_agenda, id_agenda_has_agenda, id_agenda_definition, value)
VALUES
    ('parent_has_member_has_club_and_admin_lvl3', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'parent_has_member_has_club_and_admin_lvl2'), (SELECT id FROM user_group WHERE name = 'Clenove_ClenoveKlubuAdmina_EditLimitovan+Pridani'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'club_has_administrator'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'club_administrator' AND segment_name = 'club_administrator_main' AND attribute_group_name = 'club_administrator_main' AND attribute_name = 'appuser') , null)
;

UPDATE flexii.agenda_has_agenda SET is_search_limited_by_permission_linked = false WHERE name = 'members_has_parents';

refresh materialized view mv_user_group_has_agenda_definition;
refresh materialized view mv_user_group_has_agenda_has_segment_definition;
refresh materialized view mv_user_group_has_agenda_has_segment_group_definition;
refresh materialized view mv_user_group_has_agenda_permission;

-- #15720
UPDATE agenda_definition SET is_not_null = false WHERE id IN (SELECT id FROM mv_agenda_definition WHERE full_unique_name IN ('examiner-examiner_main-examiner_main-examiner_registration_number'));
refresh materialized view mv_agenda_definition;

-- #15605
DROP MATERIALIZED VIEW flexii.lookup_judo_categories;

alter table lookup_categories drop column year_from;
alter table lookup_categories drop column year_to;
alter table lookup_categories add valid_to smallint default null;

UPDATE flexii.lookup_categories SET name = 'u8' WHERE name = '2024_u8';
UPDATE flexii.lookup_categories SET name = 'mu12' WHERE name = '2024_mu12';
UPDATE flexii.lookup_categories SET name = 'mu14' WHERE name = '2024_mu14';
UPDATE flexii.lookup_categories SET name = 'mu16' WHERE name = '2024_mu16';
UPDATE flexii.lookup_categories SET name = 'mu18' WHERE name = '2024_mu18';
UPDATE flexii.lookup_categories SET name = 'mu21' WHERE name = '2024_mu21';
UPDATE flexii.lookup_categories SET name = 'muzi' WHERE name = '2024_muzi';
UPDATE flexii.lookup_categories SET name = 'wu16' WHERE name = '2024_wu16';
UPDATE flexii.lookup_categories SET name = 'wu18' WHERE name = '2024_wu18';
UPDATE flexii.lookup_categories SET name = 'wu21' WHERE name = '2024_wu21';
UPDATE flexii.lookup_categories SET name = 'zeny' WHERE name = '2024_zeny';
UPDATE flexii.lookup_categories SET name = 'mu23' WHERE name = '2024_mu23';
UPDATE flexii.lookup_categories SET name = 'wu23' WHERE name = '2024_wu23';
UPDATE flexii.lookup_categories SET name = 'u10' WHERE name = '2024_u10';
UPDATE flexii.lookup_categories SET name = 'wu12' WHERE name = '2024_wu12';
UPDATE flexii.lookup_categories SET name = 'wu14' WHERE name = '2024_wu14';

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,
       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,
       true AS enabled,
       lc.valid_from,
       lc.valid_to
FROM lookup_categories lc
         LEFT JOIN lookup_weightcategories lw ON lc.id = lw.id_lookup_categories AND lw.enabled = TRUE
WHERE lc.enabled = TRUE;

----------------------------------------------------
-- 2.6.28.0
-- SQL for this version ends here
----------------------------------------------------

-- #15568
INSERT INTO user_group_has_definition_value_for_aha (name, id_recursion, id_user_group, id_agenda, id_agenda_has_agenda, id_agenda_definition, value)
VALUES
    ('parent_has_member2_has_club_and_admin_lvl1', null, (SELECT id FROM user_group WHERE name = 'Clenove_ClenoveKlubuAdmina_EditLimitovan+Pridani'), (SELECT id FROM agenda WHERE name = 'parents'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_parents2'), null, null)
;

INSERT INTO user_group_has_definition_value_for_aha (name, id_recursion, id_user_group, id_agenda, id_agenda_has_agenda, id_agenda_definition, value)
VALUES
    ('parent_has_member2_has_club_and_admin_lvl2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'parent_has_member2_has_club_and_admin_lvl1'), (SELECT id FROM user_group WHERE name = 'Clenove_ClenoveKlubuAdmina_EditLimitovan+Pridani'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), null, null)
;

INSERT INTO user_group_has_definition_value_for_aha (name, id_recursion, id_user_group, id_agenda, id_agenda_has_agenda, id_agenda_definition, value)
VALUES
    ('parent_has_member2_has_club_and_admin_lvl3', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'parent_has_member2_has_club_and_admin_lvl2'), (SELECT id FROM user_group WHERE name = 'Clenove_ClenoveKlubuAdmina_EditLimitovan+Pridani'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'club_has_administrator'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'club_administrator' AND segment_name = 'club_administrator_main' AND attribute_group_name = 'club_administrator_main' AND attribute_name = 'appuser') , null)
;

UPDATE flexii.agenda_has_agenda SET is_search_limited_by_permission_linked = false WHERE name = 'members_has_parents2';

UPDATE user_group_has_agenda set is_can_delete = true WHERE id_user_group = (SELECT id FROM user_group WHERE name = 'Clenove_ClenoveKlubuAdmina_EditLimitovan+Pridani')
                                                        AND id_agenda = (SELECT id FROM agenda WHERE name = 'parents');

refresh materialized view mv_user_group_has_agenda_definition;
refresh materialized view mv_user_group_has_agenda_has_segment_definition;
refresh materialized view mv_user_group_has_agenda_has_segment_group_definition;
refresh materialized view mv_user_group_has_agenda_permission;

----------------------------------------------------
-- 2.6.28.1
-- SQL for this version ends here
----------------------------------------------------

-- #15719
-- Underaged children must not have parents anymore
UPDATE flexii.agenda_definition_specification_by_value
    SET is_not_null = false

    WHERE
        id_agenda_definition = (
            SELECT id
            FROM mv_agenda_definition
            WHERE full_unique_name = 'members-members_main-members_main-members_has_parents'
        ) -- This is the definition, which should have 'is_not_null' = false
    AND
        id_agenda_definition_control = (
            SELECT id
            FROM mv_agenda_definition
            WHERE full_unique_name = 'members-members_main-members_main-nonage'
        ) -- 'is_not_null' = false is applied based on the value of this definition
    AND
        value_control::int = 1; -- 'is_not_null' = false is applied when the above definition is 1 (i.e. "true")

----------------------------------------------------
-- 2.6.29.0
-- SQL for this version ends here
----------------------------------------------------

--#15871
-- Add attributes for videos (day, tatami number, url, name, description)
INSERT INTO attribute ("name", description, data_type_precision,
                       lookup, lookup_columns, weight,
                       id_attribute_data_type)
VALUES
    (
       'tournament_video_day',
       'Den turnaje, kdy byl záznam pořízen',
       0,
       null,
       null, 1,
       (select id from attribute_data_type where name = 'int')
    ),
    (
       'tournament_video_url',
       'Odkaz na záznam na youtube.com',
       0,
       null,
       null, 1,
       (select id from attribute_data_type where name = 'url')
    ),
    (
        'tournament_video_dt_start',
        'Čas začátku videozáznamu',
        0,
        null,
        null, 1,
        (select id from attribute_data_type where name = 'datetime')
    ),
    (
       'tournament_video_tatami_number',
       'Číslo tatami, ze kterého je videozáznam',
       0,
       null,
       null, 1,
       (select id from attribute_data_type where name = 'int')
    )
;

--add recurring group for videos
INSERT INTO agenda_segment ("name", source_table, file_source_table)
VALUES
    ('tournament_videos', 'agenda_values', 'agenda_has_file');

INSERT INTO agenda_attribute_group
    ("name", is_group_recurring)
VALUES
    ('tournament_videos_main', true);

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_videos'),
     6, 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_videos')
        ),
        (select id from agenda_attribute_group where name = 'tournament_videos_main'),
        false, 1
    );

--put definitions into recurring group
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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition, min_value, max_value)
VALUES(
        (select id from attribute where name = 'tournament_video_tatami_number')
      , 10, true, NULL, false, false, NULL, 3, 0, 1
      , null, 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_videos')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_videos_main')
        ),
       1, 10
      );

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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition, min_value)
VALUES(
        (select id from attribute where name = 'tournament_video_day')
      , 20, true, NULL, false, false,
        'Pořadové číslo dne turnaje',
       3, 0, 1
      , null, 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_videos')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_videos_main')
        ),
        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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'tournament_video_dt_start')
      , 30, false, NULL, false, false,
        'Přesný čas začátku videozáznamu. Nepovinné.', 3, 0, 1
      , null, 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_videos')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_videos_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, datagrid_priority, is_search_by
                              , in_title_and_priority, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'tournament_video_url')
      , 40, true, NULL, false, false, NULL, 6, 0, 1
      , null, 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_videos')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_videos_main')
        )
      );

REFRESH MATERIALIZED VIEW mv_agenda_definition;

INSERT INTO flexii.agenda_definition_validation (id_agenda_definition, class_name, priority)
SELECT mad.id, 'YoutubeURLValidation', 10
FROM mv_agenda_definition mad
WHERE mad.full_unique_name = 'tournaments-tournament_videos-tournament_videos_main-tournament_video_url';

REFRESH MATERIALIZED VIEW mv_agenda_definition;

----------------------------------------------------
-- 2.6.31.0
-- SQL for this version ends here
----------------------------------------------------