CREATE TABLE flexii.lookup_credit_point(
  id serial NOT NULL,
  name varchar(100) NOT NULL,
  description varchar(4096) NOT NULL,
  points varchar(100) NOT NULL,
  organizer varchar(100) NOT NULL,
  grants varchar(100) NOT NULL,
  priority integer NOT NULL DEFAULT 0,
  enabled boolean NOT NULL DEFAULT true,
  dt_ins timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  dt_upd timestamp,
  CONSTRAINT pk_lookup_credit_point PRIMARY KEY (id)
);

ALTER TABLE lookup_credit_point OWNER TO flexii;

CREATE TRIGGER tr_lookup_credit_point_dt_upd
    BEFORE UPDATE
    ON flexii.lookup_credit_point
    FOR EACH ROW
EXECUTE PROCEDURE base.update_dt_upd();

INSERT INTO lookup_credit_point (name, description, points, organizer, grants, priority) VALUES('10points_ijf', '- IJF Academy', '10b', 'IJF', 'ČSJu', 10);
INSERT INTO lookup_credit_point (name, description, points, organizer, grants, priority) VALUES('8points_theory_praction', '- Min. 4 tematické části, min. 8 hod. teorie a praxe', '8b', 'ČSJu, externí organizace doporučená ČSJu', 'ČSJu', 20);
INSERT INTO lookup_credit_point (name, description, points, organizer, grants, priority) VALUES('8points_international_internship', '- Mezinárodní stáž předem schválená ČSJu, v rozsahu min. 8 hod. aktivní výuky, nutnost podat písemnou zprávu ze stáže', '8b', '', 'ČSJu', 30);
INSERT INTO lookup_credit_point (name, description, points, organizer, grants, priority) VALUES('5points_study', '- Min. 2 tematické části, min. 4 hod.', '5b', 'ČSJu', 'ČSJu', 40);
INSERT INTO lookup_credit_point (name, description, points, organizer, grants, priority) VALUES('5points_all_day_seminar', '- Min. 2 tematické části, min. 5 hod. aktivní výuky (celodenní seminář)', '5b', 'ČSJu', 'ČSJu', 50);
INSERT INTO lookup_credit_point (name, description, points, organizer, grants, priority) VALUES('3points_study', '- Min. 1 tematická část, min. 3 hod. aktivní výuky', '3b', 'ČSJu/KSJu', 'ČSJu/KSJu', 60);
INSERT INTO lookup_credit_point (name, description, points, organizer, grants, priority) VALUES('3points_seminar', '- Lektorská činnost v rámci seminářů ČSJu/KSJu, min. 1 tematická část', '3b', 'ČSJu/KSJu', 'ČSJu/KSJu', 70);
INSERT INTO lookup_credit_point (name, description, points, organizer, grants, priority) VALUES('1points_lecture', '- Online přednáška min. 1,5 hod.', '2b', 'ČSJu', 'ČSJu', 80);

-- Datum přidělení
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('date_of_assignment', 'Datum přidělení', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'date'));

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

-- číselník kreditních bodů
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('credit_point', 'Kreditní body', 0, 'lookup_credit_point', '{"id": "id", "code": "points", "name": "description"}', 1, (select id from attribute_data_type where name = 'lookup_solo_table'));

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


-- agenda Svazoví funkcionáři
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('agenda_official', 'Svazoví funcionáři', 0, 'official', NULL, 1, (select id from attribute_data_type where name = 'agenda'));

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


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

/*13947*/
delete
from agenda_definition
where id in (SELECT id
             FROM mv_agenda_definition
             WHERE agenda_name = 'tournaments'
               AND segment_name = 'tournament_access'
               AND attribute_group_name = 'tournament_manager_main'
               AND attribute_name = 'access_password');


DELETE
FROM agenda_has_segment_has_attribute_group_definition
WHERE 1 = 1
  AND id_agenda_has_segment_definition = (select id
                                          from agenda_has_segment_definition
                                          where id_agenda_segment =
                                                (select id from agenda_segment where name = 'tournament_access'))
  AND id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_manager_main');



DELETE
FROM agenda_button_has_action
WHERE id_agenda_action = (select id from agenda_action where name = 'TournamentManagerPassword');

DELETE
FROM agenda_action
where name = 'TournamentManagerPassword';

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- #13813 --
update attribute set id_attribute_data_type = (
    select id from attribute_data_type where name = 'datetime'
) where name = 'dt_term_for_registration';

-- #13709 --
-- agenda_to_agenda_sync
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 ( 'members_has_trainer_agenda_sync'
       , (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_trainer')
       , null
       , NULL
       , true
       , false
       , false
       , (select id FROM agenda WHERE name = 'members')
       );

INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES('member_trainer-id_member_id_member'
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'id_member'
       )
      , (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 = 'id_member_sync'
       )
);

INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES('member_trainer-firstname_firstname'
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'firstname'
       )
      , (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 = 'firstname'
       )
);

INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES('member_trainer-surname_surname'
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'surname'
       )
      , (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 = 'surname'
       )
);

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-id_member_id_member')
);

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-firstname_firstname')
);

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-surname_surname')
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- #13817 --

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


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

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

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

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

--Člen

UPDATE agenda_definition SET id_attribute = (select id from attribute where name = 'members_has_official'), is_not_null = false, is_in_datagrid = false
where id = (
SELECT id FROM mv_agenda_definition
WHERE agenda_name = 'official'
    AND segment_name = 'official_main'
    AND attribute_group_name = 'official_main'
    AND attribute_name = 'member'
);

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

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

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- agenda_to_agenda_sync
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 ( 'members_has_official_agenda_sync'
       , (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_official')
       , null
       , NULL
       , true
       , false
       , false
       , (select id FROM agenda WHERE name = 'members')
       );

INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES('member_official-firstname_firstname'
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'firstname'
       )
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'official'
           AND segment_name = 'official_main'
           AND attribute_group_name = 'official_member_autoinfo'
           AND attribute_name = 'firstname'
       )
);

INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES('member_official-surname_surname'
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'surname'
       )
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'official'
           AND segment_name = 'official_main'
           AND attribute_group_name = 'official_member_autoinfo'
           AND attribute_name = 'surname'
       )
);


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_official_agenda_sync')
   , (SELECT id FROM agenda_definition_to_agenda_definition_mapping WHERE name = 'member_official-firstname_firstname')
);

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_official_agenda_sync')
   , (SELECT id FROM agenda_definition_to_agenda_definition_mapping WHERE name = 'member_official-surname_surname')
);

UPDATE agenda_definition SET is_not_null = false, is_in_datagrid = false, in_title_and_priority = null
where id = (
SELECT id FROM mv_agenda_definition
WHERE agenda_name = 'official'
    AND segment_name = 'official_main'
    AND attribute_group_name = 'official_main'
    AND attribute_name = 'appuser'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

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


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

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

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

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

--Člen

UPDATE agenda_definition SET id_attribute = (select id from attribute where name = 'members_has_tournament_administrator'), is_not_null = false, is_in_datagrid = false, in_title_and_priority = null, priority = 5
where id = (
SELECT id FROM mv_agenda_definition
WHERE agenda_name = 'tournament_administrator'
    AND segment_name = 'tournament_administrator_main'
    AND attribute_group_name = 'tournament_administrator_main'
    AND attribute_name = 'member'
);

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

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

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- agenda_to_agenda_sync
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 ( 'members_has_tournament_administrator_agenda_sync'
       , (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_tournament_administrator')
       , null
       , NULL
       , true
       , false
       , false
       , (select id FROM agenda WHERE name = 'members')
       );

INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES('member_tournament_administrator-firstname_firstname'
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'firstname'
       )
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'tournament_administrator'
           AND segment_name = 'tournament_administrator_main'
           AND attribute_group_name = 'tournament_administrator_member_autoinfo'
           AND attribute_name = 'firstname'
       )
);

INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES('member_tournament_administrator-surname_surname'
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'surname'
       )
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'tournament_administrator'
           AND segment_name = 'tournament_administrator_main'
           AND attribute_group_name = 'tournament_administrator_member_autoinfo'
           AND attribute_name = 'surname'
       )
);

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_tournament_administrator_agenda_sync')
   , (SELECT id FROM agenda_definition_to_agenda_definition_mapping WHERE name = 'member_tournament_administrator-firstname_firstname')
);

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_tournament_administrator_agenda_sync')
   , (SELECT id FROM agenda_definition_to_agenda_definition_mapping WHERE name = 'member_tournament_administrator-surname_surname')
);

UPDATE agenda_definition SET is_not_null = false, is_in_datagrid = false, in_title_and_priority = null, priority = 10
where id = (
SELECT id FROM mv_agenda_definition
WHERE agenda_name = 'tournament_administrator'
    AND segment_name = 'tournament_administrator_main'
    AND attribute_group_name = 'tournament_administrator_main'
    AND attribute_name = 'appuser'
);

/*13894*/
UPDATE agenda_definition
SET is_in_datagrid = true,
    is_not_null = true
WHERE id = (SELECT id
            FROM mv_agenda_definition
            WHERE agenda_name = 'parents'
              AND segment_name = 'parents_main'
              AND attribute_group_name = 'parents_contact_main'
              AND attribute_name = 'email');

UPDATE agenda_definition
SET is_in_datagrid = true,
    is_not_null = true
WHERE id = (SELECT id
            FROM mv_agenda_definition
            WHERE agenda_name = 'parents'
              AND segment_name = 'parents_main'
              AND attribute_group_name = 'parents_contact_main'
              AND attribute_name = 'telephone');


UPDATE agenda_definition
SET is_in_datagrid = true,
    is_not_null = true,
    default_value = TO_CHAR(current_date, 'DD.MM.YYYY')
WHERE id = (SELECT id
            FROM mv_agenda_definition
            WHERE agenda_name = 'parents'
              AND segment_name = 'parents_main'
              AND attribute_group_name = 'parents_main'
              AND attribute_name = 'dt_of_registration');

UPDATE agenda_definition
SET is_in_datagrid = false,
    is_not_null = false
WHERE id = (SELECT id
            FROM mv_agenda_definition
            WHERE agenda_name = 'parents'
              AND segment_name = 'parents_main'
              AND attribute_group_name = 'parents_address_main'
              AND attribute_name = 'city');

UPDATE agenda_definition
SET is_unique = false
WHERE id = (SELECT id
            from mv_agenda_definition
            WHERE agenda_name = 'parents'
              AND attribute_group_name = 'parents_main'
              AND attribute_name = 'id_parent');


UPDATE agenda_definition
SET is_not_null = true,
    default_value = 1
WHERE id = (SELECT id
            from mv_agenda_definition
            WHERE agenda_name = 'parents'
              AND attribute_group_name = 'parents_main'
              AND attribute_name = 'is_active');



UPDATE agenda_definition
SET is_not_null = false
WHERE id = (SELECT id
            from mv_agenda_definition
            WHERE agenda_name = 'parents'
              AND attribute_group_name = 'parents_address_main'
              AND attribute_name = 'street_number');

UPDATE agenda_definition
SET is_not_null = false
WHERE id = (SELECT id
            from mv_agenda_definition
            WHERE agenda_name = 'parents'
              AND attribute_group_name = 'parents_address_main'
              AND attribute_name = 'city');


UPDATE agenda_definition
SET is_not_null = false
WHERE id = (SELECT id
            from mv_agenda_definition
            WHERE agenda_name = 'parents'
              AND attribute_group_name = 'parents_address_main'
              AND attribute_name = 'postal_code');


UPDATE agenda_definition
SET is_not_null = false
WHERE id = (SELECT id
            from mv_agenda_definition
            WHERE agenda_name = 'parents'
              AND attribute_group_name = 'parents_address_main'
              AND attribute_name = 'address_region');

UPDATE agenda_definition
SET is_not_null = false,
    default_value = 43
WHERE id = (SELECT id
            from mv_agenda_definition
            WHERE agenda_name = 'parents'
              AND attribute_group_name = 'parents_address_main'
              AND attribute_name = 'country');

UPDATE agenda_definition
SET is_read_only = true
WHERE id = (SELECT id
            from mv_agenda_definition
            WHERE agenda_name = 'parents'
              AND attribute_group_name = 'parents_contact_main'
              AND attribute_name = 'phone_office');


delete
from agenda_definition
WHERE id = (SELECT id
            FROM mv_agenda_definition
            WHERE agenda_name = 'parents'
              AND segment_name = 'parents_main'
              AND attribute_group_name = 'parents_main'
              AND attribute_name = 'dt_of_change');

CREATE SEQUENCE flexii.seq_parent_id
    INCREMENT BY 1
    MINVALUE 0
    MAXVALUE 2147483647
    START WITH 1
    CACHE 1
    NO CYCLE
    OWNED BY NONE;

ALTER SEQUENCE flexii.seq_parent_id OWNER TO flexii;

UPDATE attribute
SET custom_data_class = 'ParentId'
WHERE name = 'id_parent';

UPDATE agenda_definition
SET is_disabled = TRUE
WHERE id = (SELECT id
            FROM mv_agenda_definition
            WHERE agenda_name = 'parents'
              AND segment_name = 'parents_main'
              AND attribute_group_name = 'parents_main'
              AND attribute_name = 'id_parent');

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- #14025
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_trainer'),
        (SELECT id
         FROM agenda_has_segment_has_attribute_group_definition
         WHERE id_agenda_attribute_group = (SELECT id FROM agenda_attribute_group WHERE name = 'trainer_class_main')
           AND id_agenda_has_segment_definition = (SELECT id
                                                   FROM agenda_has_segment_definition
                                                   WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'trainer')
                                                     AND id_agenda_segment = (SELECT id FROM agenda_segment WHERE name = 'trainer_class_main'))));

-- #14035 hide aha tab --
update agenda_has_agenda set is_tab_hidden = true
where 1=1
AND name = 'members_has_official';

update agenda_has_agenda set is_tab_hidden = true
where 1=1
AND name = 'members_has_tournament_administrator';

update agenda_has_agenda set is_tab_hidden = true
where 1=1
AND name = 'members_has_trainer';

-- #13628 --

-- ID člena
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'id_member_sync')
      , 10, false, NULL, false, true, NULL, 3, 0, 1, true, true, 1, true, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'tournament_administrator')
            and id_agenda_segment = (select id from agenda_segment where name = 'tournament_administrator_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'tournament_administrator_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_tournament_administrator-id_member_id_member_sync'
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'id_member'
       )
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'tournament_administrator'
           AND segment_name = 'tournament_administrator_main'
           AND attribute_group_name = 'tournament_administrator_member_autoinfo'
           AND attribute_name = 'id_member_sync'
       )
);

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_tournament_administrator_agenda_sync')
   , (SELECT id FROM agenda_definition_to_agenda_definition_mapping WHERE name = 'member_tournament_administrator-id_member_id_member_sync')
);

-- #13817 --

-- ID člena
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'id_member_sync')
      , 10, false, NULL, false, true, NULL, 3, 0, 1, true, true, 1, true, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'official')
            and id_agenda_segment = (select id from agenda_segment where name = 'official_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'official_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_official-id_member_id_member_sync'
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'id_member'
       )
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'official'
           AND segment_name = 'official_main'
           AND attribute_group_name = 'official_member_autoinfo'
           AND attribute_name = 'id_member_sync'
       )
);

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_official_agenda_sync')
   , (SELECT id FROM agenda_definition_to_agenda_definition_mapping WHERE name = 'member_official-id_member_id_member_sync')
);

-- #13715 --
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Zkušební komisaři
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-- menu
INSERT INTO agenda  (name, icon, is_importable) VALUES('examiner', 'fad fa-user-tie-hair', false);

INSERT INTO menu_link (name, presenter, action, parameters) VALUES('examiner_default', 'Agenda:Agenda', 'default', '{"name":"examiner"}');
INSERT INTO menu_link (name, presenter, action, parameters) VALUES('examiner_detail', 'Agenda:Agenda', 'detail', '{"name":"examiner"}');

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 = 'examiner')
      , 'examiner', 'fad fa-user-tie-hair'
      , (select id from menu_link where name = 'examiner_default'), 25, false);

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

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

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

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

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

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


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

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

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

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

--Člen
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'members_has_examiner')
      , 20, true, NULL, false, false, NULL, 3, 0, 1, false, false, NULL, true, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = '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_main')
          )
      );

--Uživatel
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'appuser')
      , 25, true, NULL, false, false, NULL, 3, 0, 1, true, true, NULL, false, true
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = '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_main')
          )
      );

-- Evidenční číslo
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
        (select id from attribute where name = 'examiner_registration_number')
      , 30, true, NULL, false, false, NULL, 3, 0, 1, true, true, 4, true, true
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = '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_main')
          )
      );

-- ID člena
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'id_member_sync')
      , 5, false, NULL, false, true, NULL, 3, 0, 1, true, true, 1, true, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = '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')
          )
      );

-- Jméno člena
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'firstname')
      , 10, false, NULL, false, true, NULL, 3, 0, 1, true, true, 2, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = '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')
          )
      );

-- Příjmení člena
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'surname')
      , 15, false, NULL, false, true, NULL, 3, 0, 1, true, true, 3, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = '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')
          )
      );

-- Záložka - Třída Zkušebního komisaře
INSERT INTO agenda_segment ("name", source_table, file_source_table) VALUES('examiner_class_main', 'agenda_values', 'agenda_has_file');

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

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

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

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('examiner_class', 'Třída zkušebního komisaře', 0, 'lookup_examiner_class', NULL, 1, (select id from attribute_data_type where name = 'lookup'));

INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_examiner_class', 'third_tc', 'III. třída', 10);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_examiner_class', 'second_tc', 'II. třída', 20);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_examiner_class', 'first_tc', 'I. třída', 30);

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

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

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

-- -- Záložka - Schválené KYU
INSERT INTO agenda_segment ("name", source_table, file_source_table) VALUES('approved_kyu_main', 'agenda_values', 'agenda_has_file');

--trainer_class_main
INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('approved_kyu_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 = 'examiner')
      , (select id from agenda_segment where name = 'approved_kyu_main')
      , 10, false
      );

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


-- AHA Members has trainer
-- INSERT INTO agenda_has_agenda (name, id_agenda, id_agenda_linked, relation, is_unique, priority, is_quick_add_link, is_quick_add_link_linked, is_quick_add_agenda, is_quick_add_agenda_linked, is_custom_name)
-- VALUES (
--        'members_has_examiner'
--        , (select id from agenda where name = 'members')
--        , (select id from agenda where name = 'examiner')
--        , 3, true, 240, false, false, false, false, true);
--
-- INSERT INTO ATTRIBUTE
-- (NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
-- VALUES('members_has_examiner', 'Člen', 0, 'members_has_examiner', NULL, 1, (select id from attribute_data_type where name = 'agenda_has_agenda'));
--
-- --Členové
-- INSERT INTO agenda_has_agenda_definition (id_agenda_has_agenda, id_attribute, priority, is_not_null, is_unique, default_value) VALUES(
-- (select id from agenda_has_agenda where name = 'members_has_examiner')
-- , (select id from attribute where name = 'members_has_examiner'), 10, true, true, null);
--
-- --Člen
-- INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
--                               , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
-- VALUES(
--       (select id from attribute where name = 'members_has_examiner')
--       , 5, true, NULL, false, false, NULL, 3, 0, 1, false, false, NULL, true, false
--       , (select id from agenda_has_segment_has_attribute_group_definition
--          where id_agenda_has_segment_definition = (
--          select id from agenda_has_segment_definition
--            where id_agenda = (select id from agenda where name = 'examiner')
--             and id_agenda_segment = (select id from agenda_segment where name = 'approved_kyu_main')
--          )
--            and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'approved_kyu_main')
--           )
--       );

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

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

-- Výkonnostní stupeň KYU
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'kyu_level')
      , 10, true, NULL, false, false, NULL, 3, 0, 1, false, false, NULL, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'examiner')
            and id_agenda_segment = (select id from agenda_segment where name = 'approved_kyu_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'approved_kyu_main')
          )
      );

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


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 ( 'members_has_examiner_agenda_sync'
       , (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_examiner')
       , null
       , NULL
       , true
       , false
       , false
       , (select id FROM agenda WHERE name = 'members')
       );

INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES('member_examiner-id_member_id_member_sync'
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'id_member'
       )
      , (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 = 'id_member_sync'
       )
);

INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES('member_examiner-firstname_firstname'
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'firstname'
       )
      , (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 = 'firstname'
       )
);

INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES('member_examiner-surname_surname'
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'surname'
       )
      , (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 = 'surname'
       )
);

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-id_member_id_member_sync')
);

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-firstname_firstname')
);

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-surname_surname')
);

update agenda_has_segment_has_attribute_group_definition set priority = 2
where id_agenda_attribute_group = (
    select id from agenda_attribute_group where name = 'members_contact_main'
);

update agenda_has_segment_has_attribute_group_definition set priority = 3
where id_agenda_attribute_group = (
    select id from agenda_attribute_group where name = 'members_address_main'
);

update agenda_has_segment_has_attribute_group_definition set priority = 2
where id_agenda_attribute_group = (
    select id from agenda_attribute_group where name = 'parents_contact_main'
);

update agenda_has_segment_has_attribute_group_definition set priority = 3
where id_agenda_attribute_group = (
    select id from agenda_attribute_group where name = 'parents_address_main'
);

update agenda_has_segment_has_attribute_group_definition set priority = 2
where id_agenda_attribute_group = (
    select id from agenda_attribute_group where name = 'tournaments_address_main'
);

----------------------------------
---- Members remove tab coach ----
----------------------------------

-- agenda_values
delete from agenda_values
 where id_agenda_definition in (
 SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'members'
   AND segment_name = 'coach'
   AND attribute_group_name = 'coach'
   AND attribute_name in ('coach_level', 'dt_granted', 'dt_valid_until')
);

-- agenda_definition
delete from agenda_definition
 where id in (
 SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'members'
   AND segment_name = 'coach'
   AND attribute_group_name = 'coach'
   AND attribute_name in ('coach_level', 'dt_granted', 'dt_valid_until')
);

delete 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 = 'coach')
    )
AND id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'coach');

delete from agenda_has_segment_definition
where id in (
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 = 'coach')
);

delete from agenda_attribute_group where name = 'coach';

delete from agenda_segment where name = 'coach';

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-------------------------------------
---- Members remove tab examiner ----
-------------------------------------

-- agenda_values
delete from agenda_values
 where id_agenda_definition in (
 SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'members'
   AND segment_name = 'examiner'
   AND attribute_group_name = 'examiner'
   AND attribute_name in ('examiner_level', 'dt_granted', 'dt_valid_until', 'examiner_registration_number')
);

-- agenda_definition
delete from agenda_definition
 where id in (
 SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'members'
   AND segment_name = 'examiner'
   AND attribute_group_name = 'examiner'
   AND attribute_name in ('examiner_level', 'dt_granted', 'dt_valid_until', 'examiner_registration_number')
);

delete 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 = 'examiner')
    )
AND id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'examiner');

delete from agenda_has_segment_definition
where id in (
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 = 'examiner')
);

delete from agenda_attribute_group where name = 'examiner';

delete from agenda_segment where name = 'examiner';

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- Added recurring tab from examiner --
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_examiner'),
        (SELECT id
         FROM agenda_has_segment_has_attribute_group_definition
         WHERE id_agenda_attribute_group = (SELECT id FROM agenda_attribute_group WHERE name = 'examiner_class_main')
           AND 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_class_main')
           )
        )
);

--Zkušební komisař
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('agenda_examiner', 'Zkušební komisař', 0, 'examiner', NULL, 1, (select id from attribute_data_type where name = 'agenda'));

update agenda_definition set id_attribute = (select id from attribute where name = 'agenda_examiner'), is_not_null = true
where id = (
SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'members'
   AND segment_name = 'kyu'
   AND attribute_group_name = 'kyu'
   AND attribute_name = 'commissar'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

update MASTER_LOOKUP set name = 'kyu3_2'
where lookup_name = 'lookup_kyu_level'
AND name = 'kyu_3_2';

-- #13631 --
update agenda_definition set is_not_null = true
where id in (
    SELECT id
    FROM mv_agenda_definition
    WHERE agenda_name = 'members'
      AND segment_name = 'members_main'
      AND attribute_group_name = 'members_address_main'
      AND attribute_name in ('street', 'street_number', 'city', 'postal_code', 'address_region', 'country')
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- #13715 agenda examiner icon update --
update menu_item set icon = 'fad fa-graduation-cap' where name = 'examiner';
update agenda set icon = 'fad fa-graduation-cap' where name = 'examiner';

-- #14021
update agenda_has_agenda
set
    is_addable = false
  , is_addable_linked = false
  , is_editable_agenda = true
  , is_editable_agenda_linked = true
  , is_deletable = true
  , is_deletable_linked = true
where name = 'tournaments_has_members';



--- #13716 ---
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Rozhodčí
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-- menu
INSERT INTO agenda  (name, icon, is_importable) VALUES('referee', 'fad fa-scale-unbalanced', false);

INSERT INTO menu_link (name, presenter, action, parameters) VALUES('referee_default', 'Agenda:Agenda', 'default', '{"name":"referee"}');
INSERT INTO menu_link (name, presenter, action, parameters) VALUES('referee_detail', 'Agenda:Agenda', 'detail', '{"name":"referee"}');

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 = 'referee')
      , 'referee', 'fad fa-scale-unbalanced'
      , (select id from menu_link where name = 'referee_default'), 30, false);

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

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

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

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

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

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


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

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

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

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

--Člen
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'members_has_referee')
      , 5, true, NULL, false, false, NULL, 3, 0, 1, false, false, NULL, true, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = '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_main')
          )
      );

--Uživatel
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'appuser')
      , 10, true, NULL, false, false, NULL, 3, 0, 1, false, false, NULL, true, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = '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_main')
          )
      );

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

--Instruktor
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'is_instructor')
      , 15, false, NULL, false, true, NULL, 3, 0, 1, true, false, NULL, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = '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_main')
          )
      );

--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, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'is_active')
      , 20, true, NULL, false, false, NULL, 3, 0, 1, true, false, NULL, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = '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_main')
          )
      );

-- ID člena
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'id_member_sync')
      , 10, false, NULL, false, true, NULL, 3, 0, 1, true, true, 1, true, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = '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')
          )
      );

-- Jméno člena
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'firstname')
      , 15, false, NULL, false, true, NULL, 3, 0, 1, true, true, 2, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = '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')
          )
      );

-- Příjmení člena
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'surname')
      , 20, false, NULL, false, true, NULL, 3, 0, 1, true, true, 3, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = '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')
          )
      );

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

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

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

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

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('referee_class', 'Třída rozhodčích', 0, 'lookup_referee_class', NULL, 1, (select id from attribute_data_type where name = 'lookup'));

INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_referee_class', 'third_rc', 'III. třída', 10);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_referee_class', 'second_rc', 'II. třída', 20);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_referee_class', 'first_rc', 'I. třída', 30);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_referee_class', 'national_referee', 'NR - národní rozhodčí', 40);

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

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

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

-- vazba na svazové funkcionáře
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'agenda_official')
      , 20, true, NULL, false, false, NULL, 3, 0, 1, false, false, NULL, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'referee')
            and id_agenda_segment = (select id from agenda_segment where name = 'referee_class_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'referee_class_main')
          )
      );

-- poznámka
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'note')
      , 25, false, NULL, false, false, NULL, 6, 0, 1, false, false, NULL, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'referee')
            and id_agenda_segment = (select id from agenda_segment where name = 'referee_class_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'referee_class_main')
          )
      );

-- Záložka - Třída mezinárodního rozhodčího
INSERT INTO agenda_segment ("name", source_table, file_source_table) VALUES('international_referee_class_main', 'agenda_values', 'agenda_has_file');

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

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

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

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('international_referee_class', 'Třída mezinárodního rozhodčího', 0, 'lookup_international_referee_class', NULL, 1, (select id from attribute_data_type where name = 'lookup'));

INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_international_referee_class', 'ir_ijfa', 'Mezinárodní - IJF-A', 10);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_international_referee_class', 'ir_ijfb', 'Mezinárodní - IJF-B', 20);

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

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

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

-- vazba na svazové funkcionáře
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'agenda_official')
      , 20, true, NULL, false, false, NULL, 3, 0, 1, false, false, NULL, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'referee')
            and id_agenda_segment = (select id from agenda_segment where name = 'international_referee_class_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'international_referee_class_main')
          )
      );

-- poznámka
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'note')
      , 25, false, NULL, false, false, NULL, 6, 0, 1, false, false, NULL, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'referee')
            and id_agenda_segment = (select id from agenda_segment where name = 'international_referee_class_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'international_referee_class_main')
          )
      );

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

--trainer_class_main
INSERT INTO agenda_attribute_group ("name", is_group_recurring, id_agenda_attribute_group_interlaced)
VALUES('instructor_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 = 'referee')
      , (select id from agenda_segment where name = 'instructor_main')
      , 15, 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 = 'referee')
             and id_agenda_segment = (select id from agenda_segment where name = 'instructor_main')
          )
      , (select id from agenda_attribute_group where name = 'instructor_main')
      , false, 15
      );

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

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

-- vazba na svazové funkcionáře
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'agenda_official')
      , 15, true, NULL, false, false, NULL, 3, 0, 1, false, false, NULL, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = 'referee')
            and id_agenda_segment = (select id from agenda_segment where name = 'instructor_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'instructor_main')
          )
      );

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

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- #13709 --
-- agenda_to_agenda_sync
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 ( 'members_has_referee_agenda_sync'
       , (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_referee')
       , null
       , NULL
       , true
       , false
       , false
       , (select id FROM agenda WHERE name = 'members')
       );

INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES('member_referee-id_member_id_member'
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'id_member'
       )
      , (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 = 'id_member_sync'
       )
);

INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES('member_referee-firstname_firstname'
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'firstname'
       )
      , (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 = 'firstname'
       )
);

INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES('member_referee-surname_surname'
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'surname'
       )
      , (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 = 'surname'
       )
);

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-id_member_id_member')
);

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-firstname_firstname')
);

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-surname_surname')
);

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_class_main')
           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_class_main')
           )
        )
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-------------------------------------
---- Members remove tab referee ----
-------------------------------------

-- agenda_values
delete from agenda_values
 where id_agenda_definition in (
 SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'members'
   AND segment_name = 'referee'
   AND attribute_group_name = 'referee'
   AND attribute_name in ('dt_granted', 'dt_valid_until', 'referee_level')
);

-- agenda_definition
delete from agenda_definition
 where id in (
 SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'members'
   AND segment_name = 'referee'
   AND attribute_group_name = 'referee'
   AND attribute_name in ('dt_granted', 'dt_valid_until', 'referee_level')
);

delete 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 = 'referee')
    )
AND id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'referee');

delete from agenda_has_segment_definition
where id in (
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 = 'referee')
);

delete from agenda_attribute_group where name = 'referee';

delete from agenda_segment where name = 'referee';

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- 13606

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

INSERT INTO menu_link (name, presenter, action, parameters) VALUES('club_administrator_default', 'Agenda:Agenda', 'default', '{"name":"club_administrator"}');
INSERT INTO menu_link (name, presenter, action, parameters) VALUES('club_administrator_detail', 'Agenda:Agenda', 'detail', '{"name":"club_administrator"}');

INSERT INTO menu_item (id_menu_section, id_agenda, name, icon, id_menu_link, priority, is_default)
VALUES(
          (select id from menu_section where name = 'basic_evidence')
      , (select id from agenda where name = 'club_administrator')
      , 'club_administrator', 'fad fa-user-cog'
      , (select id from menu_link where name = 'club_administrator_default'), 40, false);


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



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


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


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


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

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_tab_hidden, is_tab_hidden_linked)
VALUES ( 'members_has_club_administrator'
       , (select id from agenda where name = 'members')
       , (select id from agenda where name = 'club_administrator')
       , 3, true, 250, false, false, false, false, true, false, false);

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

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

INSERT INTO agenda_definition ( id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height
                              , is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable
                              , id_agenda_segment_attribute_group_definition)
VALUES ( (select id from attribute where name = 'members_has_club_administrator')
       , 10, true, NULL, false, false, NULL, 3, 0, 1, true, 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 = 'club_administrator')
                                                      and id_agenda_segment =
                                                          (select id from agenda_segment where name = 'club_administrator_main'))
            and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'club_administrator_main')));



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

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


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

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 ( 'club_has_administrator'
       , (select id from agenda where name = 'clubs')
       , (select id from agenda where name = 'club_administrator')
       , 2, false, 260, false, false, false, false, true);


INSERT INTO agenda_has_agenda_definition (id_agenda_has_agenda, id_attribute, priority, is_not_null, is_unique, default_value)
VALUES ( (SELECT id FROM agenda_has_agenda WHERE name = 'club_has_administrator')
       , (SELECT id FROM attribute WHERE name = 'dt_valid_from'), 20, TRUE, false, NULL);

INSERT INTO agenda_has_agenda_definition (id_agenda_has_agenda, id_attribute, priority, is_not_null, is_unique, default_value)
VALUES ( (SELECT id FROM agenda_has_agenda WHERE name = 'club_has_administrator')
       , (SELECT id FROM attribute WHERE name = 'dt_valid_to'), 30, FALSE, false, NULL);

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


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 = 'club_administrator')
             and id_agenda_segment = (select id from agenda_segment where name = 'club_administrator_main')
          )
      , (select id from agenda_attribute_group where name = 'club_administrator_autoinfo')
      , false, 2
      );


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

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

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

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 ( 'members_has_club_administrator_agenda_sync'
       , (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_club_administrator')
       , null
       , NULL
       , true
       , false
       , false
       , (select id FROM agenda WHERE name = 'members')
       );
refresh materialized view mv_agenda_definition;


INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES('member_members_has_club_administrator-id_member'
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'id_member'
       )
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'club_administrator'
           AND segment_name = 'club_administrator_main'
           AND attribute_group_name = 'club_administrator_autoinfo'
           AND attribute_name = 'id_member_sync'
       )
      );

INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES('member_club_administrator-firstname_firstname'
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'firstname'
       )
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'club_administrator'
           AND segment_name = 'club_administrator_main'
           AND attribute_group_name = 'club_administrator_autoinfo'
           AND attribute_name = 'firstname'
       )
      );

INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES('member_club_administrator-surname_surname'
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'members'
           AND segment_name = 'members_main'
           AND attribute_group_name = 'members_main'
           AND attribute_name = 'surname'
       )
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'club_administrator'
           AND segment_name = 'club_administrator_main'
           AND attribute_group_name = 'club_administrator_autoinfo'
           AND attribute_name = 'surname'
       )
      );

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_club_administrator_agenda_sync')
       , (SELECT id FROM agenda_definition_to_agenda_definition_mapping WHERE name = 'member_members_has_club_administrator-id_member')
       );

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_club_administrator_agenda_sync')
       , (SELECT id FROM agenda_definition_to_agenda_definition_mapping WHERE name = 'member_club_administrator-firstname_firstname')
       );

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_club_administrator_agenda_sync')
       , (SELECT id FROM agenda_definition_to_agenda_definition_mapping WHERE name = 'member_club_administrator-surname_surname')
       );

refresh materialized view mv_agenda_definition;

-- menu item position --
update menu_item set priority = 110 where id = (
    select id from menu_item
    where 1=1
    AND id_menu_section = (select id from menu_section where name = 'basic_evidence')
    AND name = 'members'
);

update menu_item set priority = 120 where id = (
    select id from menu_item
    where 1=1
    AND id_menu_section = (select id from menu_section where name = 'basic_evidence')
    AND name = 'parents'
);

update menu_item set priority = 130 where id = (
    select id from menu_item
    where 1=1
    AND id_menu_section = (select id from menu_section where name = 'basic_evidence')
    AND name = 'clubs'
);

update menu_item set priority = 140 where id = (
    select id from menu_item
    where 1=1
    AND id_menu_section = (select id from menu_section where name = 'basic_evidence')
    AND name = 'club_administrator'
);

update menu_item set priority = 150 where id = (
    select id from menu_item
    where 1=1
    AND id_menu_section = (select id from menu_section where name = 'basic_evidence')
    AND name = 'competitions'
);

update menu_item set priority = 160 where id = (
    select id from menu_item
    where 1=1
    AND id_menu_section = (select id from menu_section where name = 'basic_evidence')
    AND name = 'official'
);

update menu_item set priority = 170 where id = (
    select id from menu_item
    where 1=1
    AND id_menu_section = (select id from menu_section where name = 'basic_evidence')
    AND name = 'trainer'
);

update menu_item set priority = 180 where id = (
    select id from menu_item
    where 1=1
    AND id_menu_section = (select id from menu_section where name = 'basic_evidence')
    AND name = 'examiner'
);

update menu_item set priority = 190 where id = (
    select id from menu_item
    where 1=1
    AND id_menu_section = (select id from menu_section where name = 'basic_evidence')
    AND name = 'referee'
);

-- menu item set new priority value --
-- menu item position --
update menu_item set priority = 10 where id = (
    select id from menu_item
    where 1=1
    AND id_menu_section = (select id from menu_section where name = 'basic_evidence')
    AND name = 'members'
);

update menu_item set priority = 20 where id = (
    select id from menu_item
    where 1=1
    AND id_menu_section = (select id from menu_section where name = 'basic_evidence')
    AND name = 'parents'
);

update menu_item set priority = 30 where id = (
    select id from menu_item
    where 1=1
    AND id_menu_section = (select id from menu_section where name = 'basic_evidence')
    AND name = 'clubs'
);

update menu_item set priority = 40 where id = (
    select id from menu_item
    where 1=1
    AND id_menu_section = (select id from menu_section where name = 'basic_evidence')
    AND name = 'club_administrator'
);

update menu_item set priority = 50 where id = (
    select id from menu_item
    where 1=1
    AND id_menu_section = (select id from menu_section where name = 'basic_evidence')
    AND name = 'competitions'
);

update menu_item set priority = 60 where id = (
    select id from menu_item
    where 1=1
    AND id_menu_section = (select id from menu_section where name = 'basic_evidence')
    AND name = 'official'
);

update menu_item set priority = 70 where id = (
    select id from menu_item
    where 1=1
    AND id_menu_section = (select id from menu_section where name = 'basic_evidence')
    AND name = 'trainer'
);

update menu_item set priority = 80 where id = (
    select id from menu_item
    where 1=1
    AND id_menu_section = (select id from menu_section where name = 'basic_evidence')
    AND name = 'examiner'
);

update menu_item set priority = 90 where id = (
    select id from menu_item
    where 1=1
    AND id_menu_section = (select id from menu_section where name = 'basic_evidence')
    AND name = 'referee'
);

-- #13600 --
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Svazové poplatky
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-- menu
INSERT INTO agenda  (name, icon, is_importable) VALUES('union_fees', 'fad fa-money-bill', false);

INSERT INTO menu_link (name, presenter, action, parameters) VALUES('union_fees_default', 'Agenda:Agenda', 'default', '{"name":"union_fees"}');
INSERT INTO menu_link (name, presenter, action, parameters) VALUES('union_fees_detail', 'Agenda:Agenda', 'detail', '{"name":"union_fees"}');

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 = 'union_fees')
      , 'union_fees', 'fad fa-money-bill'
      , (select id from menu_link where name = 'union_fees_default'), 100, false);


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



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


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


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


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

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_tab_hidden_linked, is_editable_agenda, is_editable_agenda_linked)
VALUES ( 'clubs_has_union_fees'
       , (select id from agenda where name = 'clubs')
       , (select id from agenda where name = 'union_fees')
       ,2, false, 150, false, false, false, false, false, true, false, false);



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


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


INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES ('members_number', 'Počet členů', 0, NULL, 1, (select id from attribute_data_type where name = 'int'));



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


INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES ('variable_symbol', 'Variabilní symbol', 0, NULL, 1, (select id from attribute_data_type where name = 'int'));


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

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES ('dt_of_payment_issue', 'Datum vystavení pl. příkazu', 0, NULL, 1, (select id from attribute_data_type where name = 'date'));


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


INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES ('year_fee', 'Poplatky za rok', 0, NULL, 1, (select id from attribute_data_type where name = 'year'));


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

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES ('total_amount', 'Celková částka', 0, NULL, 1, (select id from attribute_data_type where name = 'float'));


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


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

INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY)
VALUES ('lookup_states_of_payment', 'st1', 'Zaplaceno', 10);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY)
VALUES ('lookup_states_of_payment', 'st2', 'Nezaplaceno', 20);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY)
VALUES ('lookup_states_of_payment', 'st3', 'Zrušeno', 30);


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


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


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

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


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


INSERT INTO agenda_has_agenda (name, id_agenda, id_agenda_linked, relation, is_unique, priority, is_quick_add_link,
                               is_quick_add_link_linked, is_quick_add_agenda, is_quick_add_agenda_linked,
                               is_custom_name)
VALUES ( 'members_has_union_fees'
       , (select id from agenda where name = 'members')
       , (select id from agenda where name = 'union_fees')
       , 2, false, 270, false, false, false, false, true);

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES ('fee_amount', 'Výše poplatku', 0, NULL, 1, (select id from attribute_data_type where name = 'money'));

INSERT INTO agenda_has_agenda_definition (id_agenda_has_agenda, id_attribute, priority, is_not_null, is_unique, default_value)
VALUES ( (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_union_fees')
       , (SELECT id FROM attribute WHERE name = 'fee_amount'), 25, FALSE, false, 300);


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

INSERT INTO agenda_has_agenda_definition (id_agenda_has_agenda, id_attribute, priority, is_not_null, is_unique,
                                          default_value)
VALUES ( (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
       , (select id from attribute where name = 'members_number'), 20, true, true, null);

INSERT INTO agenda_has_agenda_definition (id_agenda_has_agenda, id_attribute, priority, is_not_null, is_unique,
                                          default_value)
VALUES ( (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
       , (select id from attribute where name = 'variable_symbol'), 30, true, true, null);

INSERT INTO agenda_has_agenda_definition (id_agenda_has_agenda, id_attribute, priority, is_not_null, is_unique,
                                          default_value)
VALUES ( (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
       , (select id from attribute where name = 'total_amount'), 40, true, true, null);

INSERT INTO agenda_has_agenda_definition (id_agenda_has_agenda, id_attribute, priority, is_not_null, is_unique,
                                          default_value)
VALUES ( (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
       , (select id from attribute where name = 'dt_of_payment_issue'), 50, true, true, null);

INSERT INTO agenda_has_agenda_definition (id_agenda_has_agenda, id_attribute, priority, is_not_null, is_unique,
                                          default_value)
VALUES ( (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
       , (select id from attribute where name = 'year_fee'), 60, true, true, null);

INSERT INTO agenda_has_agenda_definition (id_agenda_has_agenda, id_attribute, priority, is_not_null, is_unique,
                                          default_value)
VALUES ( (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
       , (select id from attribute where name = 'state_of_payment'), 70, true, true, null);

INSERT INTO agenda_has_agenda_definition (id_agenda_has_agenda, id_attribute, priority, is_not_null, is_unique,
                                          default_value)
VALUES ( (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
       , (select id from attribute where name = 'proforma'), 80, true, true, null);

INSERT INTO agenda_has_agenda_definition (id_agenda_has_agenda, id_attribute, priority, is_not_null, is_unique,
                                          default_value)
VALUES ( (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
       , (select id from attribute where name = 'invoice'), 90, true, true, null);

INSERT INTO agenda_action (name, description, class_name)
VALUES ('UnionFeePayment', 'Zaplaceno', 'UnionFeePaymentAction');

INSERT INTO agenda_button (id_agenda, name, description, icon, class)
VALUES ((SELECT id FROM agenda WHERE name = 'union_fees'), 'UnionFeePayment', 'Zaplaceno', 'fal fa-key', 'btn-info');

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

INSERT INTO agenda_action (name, description, class_name)
VALUES ('UnionFeeCancel', 'Zrušit', 'UnionFeeCancelAction');

INSERT INTO agenda_button (id_agenda, name, description, icon, class, confirmation)
VALUES ((SELECT id FROM agenda WHERE name = 'union_fees'), 'UnionFeeCancel', 'Zrušit', 'fal fa-key', 'btn-danger', 'Tuto akci nebude možné vrátit zpět, chcete pokračovat?');

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


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

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- agenda_values
delete from agenda_values
 where id_agenda_definition in (
 SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'members'
   AND segment_name = 'dan'
   AND attribute_group_name = 'dan'
   AND attribute_name = 'commissar'
);

-- update agenda_definiton
update agenda_definition set id_attribute = (select id from attribute where name = 'agenda_official')
 where id = (
 SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'members'
   AND segment_name = 'dan'
   AND attribute_group_name = 'dan'
   AND attribute_name = 'commissar'
);

-- delete attribute
delete from attribute where name = 'commissar';

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- menu icon + agenda changes
-- union_fees
update menu_item set icon = 'fad fa-file-invoice-dollar' where name = 'union_fees';
update agenda set icon = 'fad fa-file-invoice-dollar' where name = 'union_fees';

-- clubs
update menu_item set icon = 'fad fa-people-roof' where name = 'clubs';
update agenda set icon = 'fad fa-people-roof' where name = 'clubs';

-- club_administrator
update menu_item set icon = 'fad fa-users-gear' where name = 'club_administrator';
update agenda set icon = 'fad fa-users-gear' where name = 'club_administrator';

-- competitions
update menu_item set icon = 'fad fa-list-ol' where name = 'competitions';
update agenda set icon = 'fad fa-list-ol' where name = 'competitions';

-- tournaments
update menu_item set icon = 'fad fa-trophy' where name = 'tournaments';
update agenda set icon = 'fad fa-trophy' where name = 'tournaments';


-- added sequence - agenda trainer, examiner referee
-- Trainer
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('id_trainer', 'ID trenéra', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'int'));

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

REFRESH MATERIALIZED VIEW mv_agenda_definition;

---------------------
-- Examiner
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('id_examiner', 'ID zkušebního komisaře', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'int'));

--ID Zkušebního komisaře
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'id_examiner')
      , 2, true, NULL, true, false, NULL, 3, 0, 1, false, false, NULL, true, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = '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_main')
          )
      );

REFRESH MATERIALIZED VIEW mv_agenda_definition;

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

UPDATE attribute SET custom_data_class = 'ExaminerId' WHERE name = 'id_examiner';

---------------------
-- Referee
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('id_referee', 'ID rozhodčího', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'int'));

--ID Trenéra
INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
      (select id from attribute where name = 'id_referee')
      , 2, true, NULL, true, false, NULL, 3, 0, 1, false, false, NULL, true, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = '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_main')
          )
      );

REFRESH MATERIALIZED VIEW mv_agenda_definition;


-- #13715 - fix lookup_kyu_level
DELETE FROM master_lookup WHERE lookup_name = 'lookup_kyu_level';

CREATE TABLE flexii.lookup_kyu_level
(
    id          serial       NOT NULL,
    name        varchar(100) NOT NULL,
    description varchar(100),
    main_level smallint NOT NULL,
    priority    integer      NOT NULL,
    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_kyu_level PRIMARY KEY (id)
);

create unique index unq_lookup_kyu_level_priority
    on lookup_kyu_level (priority);

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

INSERT INTO flexii.lookup_kyu_level (NAME, DESCRIPTION, main_level, PRIORITY) VALUES('kyu6', '6', 6, 10);
INSERT INTO flexii.lookup_kyu_level (NAME, DESCRIPTION, main_level, PRIORITY) VALUES('kyu6_5', '6/5', 5, 20);
INSERT INTO flexii.lookup_kyu_level (NAME, DESCRIPTION, main_level, PRIORITY) VALUES('kyu5', '5', 5, 30);
INSERT INTO flexii.lookup_kyu_level (NAME, DESCRIPTION, main_level, PRIORITY) VALUES('kyu5_4', '5/4', 4, 40);
INSERT INTO flexii.lookup_kyu_level (NAME, DESCRIPTION, main_level, PRIORITY) VALUES('kyu4', '4', 4, 50);
INSERT INTO flexii.lookup_kyu_level (NAME, DESCRIPTION, main_level, PRIORITY) VALUES('kyu4_3', '4/3', 3, 60);
INSERT INTO flexii.lookup_kyu_level (NAME, DESCRIPTION, main_level, PRIORITY) VALUES('kyu3', '3', 3, 70);
INSERT INTO flexii.lookup_kyu_level (NAME, DESCRIPTION, main_level, PRIORITY) VALUES('kyu3_2', '3/2', 2, 75);
INSERT INTO flexii.lookup_kyu_level (NAME, DESCRIPTION, main_level, PRIORITY) VALUES('kyu2', '2', 2, 80);
INSERT INTO flexii.lookup_kyu_level (NAME, DESCRIPTION, main_level, PRIORITY) VALUES('kyu1', '1', 1, 90);

UPDATE attribute SET id_attribute_data_type = (select id from attribute_data_type where name = 'lookup_solo_table') WHERE LOOKUP = 'lookup_kyu_level';
DELETE FROM admin_editable_lookup WHERE name = 'lookup_kyu_level';

REFRESH MATERIALIZED VIEW mv_agenda_definition;

DELETE FROM agenda_values WHERE id_agenda_definition IN (SELECT id FROM mv_agenda_definition WHERE attribute_name IN (select name FROM attribute WHERE lookup = 'lookup_kyu_level'));

-- #13716
INSERT INTO agenda_has_custom_class (id_agenda, custom_class, is_on_create, is_on_edit, is_on_delete, is_on_load)
VALUES((SELECT id FROM agenda WHERE name = 'referee'), 'IsInstructor', true, true, false, false);


update agenda_definition set default_value = 1 where id in (
    SELECT id FROM mv_agenda_definition
     WHERE agenda_name = 'referee'
       AND segment_name = 'referee_main'
       AND attribute_group_name = 'referee_main'
       AND attribute_name = 'is_active'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

------------------------------------------
---- Examiner remove tab approved KYU ----
------------------------------------------

-- agenda_values
delete from agenda_values
 where id_agenda_definition in (
 SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'examiner'
   AND segment_name = 'approved_kyu_main'
   AND attribute_group_name = 'approved_kyu_main'
   AND attribute_name in ('kyu_level', 'date_of_assignment', 'member_approved_kyu')
);

-- agenda_definition
delete from agenda_definition
 where id in (
 SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'examiner'
   AND segment_name = 'approved_kyu_main'
   AND attribute_group_name = 'approved_kyu_main'
   AND attribute_name in ('kyu_level', 'date_of_assignment', 'member_approved_kyu')
);

delete 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 = 'approved_kyu_main')
    )
AND id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'approved_kyu_main');

delete from agenda_has_segment_definition
where id in (
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 = 'approved_kyu_main')
);

delete from agenda_attribute_group where name = 'approved_kyu_main';

delete from agenda_segment where name = 'approved_kyu_main';

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

UPDATE attribute SET custom_data_class = 'RefereeId' WHERE name = 'id_referee';

----------------------------------------------------
-- 2.6.2.0
-- SQL for this version ends here
----------------------------------------------------
DELETE FROM master_lookup WHERE lookup_name = 'lookup_club_officials_role' and name in ('cor2', 'cor7', 'cor4');

-- Limit administrátorů turnaje
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('tournament_administrator_limit', 'Limit administrátorů turnaje', 0, NULL, NULL, 1, (select id from attribute_data_type where name = 'int'));

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

REFRESH MATERIALIZED VIEW mv_agenda_definition;

UPDATE agenda_has_agenda SET relation = 5
       , is_unique = true
       , id_agenda_definition_max_count_limit = (
           SELECT id FROM mv_agenda_definition
             WHERE agenda_name = 'tournaments'
               AND segment_name = 'tournament_information'
               AND attribute_group_name = 'tournament_administrator_info_main'
               AND attribute_name = 'tournament_administrator_limit'
        )
WHERE name = 'tournaments_has_tournament_administrator';

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

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- remove agenda_definition with attribute tournament_administrator 2 and 3 --

-- agenda_values
delete from agenda_values
 where id_agenda_definition in (
 SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'tournaments'
   AND segment_name = 'tournament_information'
   AND attribute_group_name = 'tournament_administrator_info_main'
   AND attribute_name in ('agenda_tournament_administrator_2', 'agenda_tournament_administrator_3')
);

-- agenda_definition
delete from agenda_definition
 where id in (
 SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'tournaments'
   AND segment_name = 'tournament_information'
   AND attribute_group_name = 'tournament_administrator_info_main'
   AND attribute_name in ('agenda_tournament_administrator_2', 'agenda_tournament_administrator_3')
);

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

UPDATE agenda_definition set is_disabled = false, is_hidden = false
where id = (
 SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'tournaments'
   AND segment_name = 'tournament_information'
   AND attribute_group_name = 'tournament_administrator_info_main'
   AND attribute_name = 'tournament_administrator_limit'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;


UPDATE agenda_definition SET in_title_and_priority = 40, is_search_by = true
where id = (
SELECT id FROM mv_agenda_definition
WHERE agenda_name = 'members'
   AND segment_name = 'members_main'
   AND attribute_group_name = 'members_main'
   AND attribute_name = 'dt_birthyear'
);

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 = 'international_referee_class_main')
           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 = 'international_referee_class_main')
           )
        )
);

update agenda_definition set is_search_by = false
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'
);

update agenda_definition set is_search_by = false
where id = (
 SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'referee'
   AND segment_name = 'referee_main'
   AND attribute_group_name = 'referee_main'
   AND attribute_name = 'appuser'
);

update agenda_definition set is_search_by = false
where id = (
 SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'official'
   AND segment_name = 'official_main'
   AND attribute_group_name = 'official_main'
   AND attribute_name = 'appuser'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- #14021
update agenda_has_agenda
set
    is_addable = false
  , is_addable_linked = false
  , is_editable_agenda = true
  , is_editable_agenda_linked = true
  , is_deletable = true
  , is_deletable_linked = true
where name = 'tournaments_has_members';

--
-- Infomrace o turnaji --
-- Hlavní rozhodčí --
--

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

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

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('agenda_tournament_referee', 'Hlavní rozhodčí', 0, 'referee', NULL, 1, (select id from attribute_data_type where name = 'agenda'));


INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'agenda_tournament_referee')
      , 10, true, null, false, false, NULL, 3, 0, 1, false, false, null, false, false
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (
         select id from agenda_has_segment_definition
           where id_agenda = (select id from agenda where name = '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_referee_main')
          )
      );

REFRESH MATERIALIZED VIEW mv_agenda_definition;


-- tournament info segment priority update --

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

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

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


-- agenda referee priority --
update agenda_definition set priority = 20
where id = (
SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'referee'
   AND segment_name = 'referee_main'
   AND attribute_group_name = 'referee_main'
   AND attribute_name = 'members_has_referee'
);

update agenda_definition set priority = 25
where id = (
SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'referee'
   AND segment_name = 'referee_main'
   AND attribute_group_name = 'referee_main'
   AND attribute_name = 'appuser'
);

update agenda_definition set priority = 30
where id = (
SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'referee'
   AND segment_name = 'referee_main'
   AND attribute_group_name = 'referee_main'
   AND attribute_name = 'is_instructor'
);

update agenda_definition set priority = 35
where id = (
SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'referee'
   AND segment_name = 'referee_main'
   AND attribute_group_name = 'referee_main'
   AND attribute_name = 'is_active'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;


-- tournament_administrator - telephone is not null
update agenda_definition set is_not_null = true
where id = (
SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'tournament_administrator'
   AND segment_name = 'tournament_administrator_main'
   AND attribute_group_name = 'tournament_administrator_main'
   AND attribute_name = 'telephone'
);

-- members hosting, competitions set not null
update agenda_definition set is_not_null = true
where id = (
SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'tournament_administrator'
   AND segment_name = 'tournament_administrator_main'
   AND attribute_group_name = 'tournament_administrator_main'
   AND attribute_name = 'telephone'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

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

-- #12397 competitions changes --

-- agenda_values
DELETE FROM agenda_values
 WHERE id_agenda_definition IN (
 SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'competitions'
   AND segment_name = 'competitions_main'
   AND attribute_group_name = 'competitions_main'
   AND attribute_name in (
          'competition_type_of_championship'
        , 'competition_type_of_subcategory'
        , 'competition_team_category'
        , 'competition_sub_category'
        , 'competition_basic_category'
   )
);

-- master_lookup
DELETE FROM master_lookup WHERE lookup_name IN (
    select lookup from attribute
      where name in (
              'competition_type_of_championship'
            , 'competition_type_of_subcategory'
            , 'competition_team_category'
            , 'competition_sub_category'
            , 'competition_basic_category'
      )
);

-- agenda_definition_specification_by_value
DELETE FROM agenda_definition_specification_by_value
 WHERE id_agenda_definition IN (
 SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'competitions'
   AND segment_name = 'competitions_main'
   AND attribute_group_name = 'competitions_main'
      AND attribute_name in (
          'competition_type_of_championship'
        , 'competition_type_of_subcategory'
        , 'competition_team_category'
        , 'competition_sub_category'
        , 'competition_basic_category'
   )
);

-- agenda_definition
DELETE FROM agenda_definition
 WHERE id IN (
 SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'competitions'
   AND segment_name = 'competitions_main'
   AND attribute_group_name = 'competitions_main'
      AND attribute_name in (
          'competition_type_of_championship'
        , 'competition_type_of_subcategory'
        , 'competition_team_category'
        , 'competition_sub_category'
        , 'competition_basic_category'
   )
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- competition
-- číselník typu soutěže
CREATE TABLE flexii.lookup_competition_type(
  id serial NOT NULL,
  name varchar(100) NOT NULL,
  cat1 varchar(400) NOT NULL,
  cat2 varchar(400) NOT NULL,
  cat3 varchar(400),
  cat4 varchar(400),
  cat5 varchar(400),
  priority integer NOT NULL DEFAULT 0,
  enabled boolean NOT NULL DEFAULT true,
  dt_ins timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  dt_upd timestamp,
  CONSTRAINT pk_lookup_competition_type PRIMARY KEY (id)
);

ALTER TABLE lookup_competition_type OWNER TO flexii;

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

-- mistrovské souteže --
INSERT INTO lookup_competition_type (name, cat1, cat2, cat3, cat4, cat5, priority) VALUES('mis_mcr_jdn_jdt', 'mistrovské', 'mčr', 'jednorázové', 'jednotlivci', null, 10);
INSERT INTO lookup_competition_type (name, cat1, cat2, cat3, cat4, cat5, priority) VALUES('mis_mcr_jdn_drz', 'mistrovské', 'mčr', 'jednorázové', 'družstva', null, 20);
INSERT INTO lookup_competition_type (name, cat1, cat2, cat3, cat4, cat5, priority) VALUES('mis_mcr_dlou_extrm', 'mistrovské', 'mčr', 'dlouhodobé', 'extraliga mužů', null, 30);
INSERT INTO lookup_competition_type (name, cat1, cat2, cat3, cat4, cat5, priority) VALUES('mis_mcr_dlou_doros', 'mistrovské', 'mčr', 'dlouhodobé', 'dorostenecká liga', null, 40);
INSERT INTO lookup_competition_type (name, cat1, cat2, cat3, cat4, cat5, priority) VALUES('mis_mcr_pred', 'mistrovské', 'mčr', 'předváděcí', null, null, 50);
INSERT INTO lookup_competition_type (name, cat1, cat2, cat3, cat4, cat5, priority) VALUES('mis_pcr_jdt', 'mistrovské', 'pčr', 'jednotlivci', null, null, 60);
INSERT INTO lookup_competition_type (name, cat1, cat2, cat3, cat4, cat5, priority) VALUES('mis_pcr_drz', 'mistrovské', 'pčr', 'družstva', null, null, 70);
INSERT INTO lookup_competition_type (name, cat1, cat2, cat3, cat4, cat5, priority) VALUES('mis_pcr_pred', 'mistrovské', 'pčr', 'předváděcí', null, null, 80);
INSERT INTO lookup_competition_type (name, cat1, cat2, cat3, cat4, cat5, priority) VALUES('mis_kp_jdt', 'mistrovské', 'pčr', 'předváděcí', null, null, 90);
INSERT INTO lookup_competition_type (name, cat1, cat2, cat3, cat4, cat5, priority) VALUES('mis_kp_drz', 'mistrovské', 'pčr', 'předváděcí', null, null, 100);

-- nemistrovksé souteže --
INSERT INTO lookup_competition_type (name, cat1, cat2, cat3, cat4, cat5, priority) VALUES('nemis_jdt_cp', 'nemistrovské', 'jednotlivci', 'čp', null, null, 110);
INSERT INTO lookup_competition_type (name, cat1, cat2, cat3, cat4, cat5, priority) VALUES('nemis_jdt_kraj', 'nemistrovské', 'jednotlivci', 'krajské', null, null, 120);
INSERT INTO lookup_competition_type (name, cat1, cat2, cat3, cat4, cat5, priority) VALUES('nemis_drz_1lig', 'nemistrovské', 'družstva', '1.liga', null, null, 130);
INSERT INTO lookup_competition_type (name, cat1, cat2, cat3, cat4, cat5, priority) VALUES('nemis_drz_bar', 'nemistrovské', 'družstva', 'baráž', null, null, 140);
INSERT INTO lookup_competition_type (name, cat1, cat2, cat3, cat4, cat5, priority) VALUES('nemis_drz_kraj', 'nemistrovské', 'družstva', 'krajské', null, null, 150);
INSERT INTO lookup_competition_type (name, cat1, cat2, cat3, cat4, cat5, priority) VALUES('nemis_pred', 'nemistrovské', 'předváděcí', null, null, null, 160);

-- Typ soutěže
INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('competition_type', 'Typ soutěže', 0, 'lookup_competition_type', '{"id": "id", "name": "description"}', 1, (select id from attribute_data_type where name = 'lookup_solo_table'));

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

update agenda_definition set priority = 10
where id = (
SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'competitions'
   AND segment_name = 'competitions_main'
   AND attribute_group_name = 'competitions_main'
   AND attribute_name = 'name'
);

update agenda_definition set priority = 15
where id = (
SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'competitions'
   AND segment_name = 'competitions_main'
   AND attribute_group_name = 'competitions_main'
   AND attribute_name = 'dt_season_year'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

delete from agenda_has_agenda_definition
 where id_attribute = (
     select id from attribute where name = 'weightcategories'
 )
 AND id_agenda_has_agenda = (
    select  id from agenda_has_agenda where name = 'tournaments_has_competitions'
);

delete from agenda_has_agenda_show_attribute
 where id_agenda_definition = (
    SELECT id FROM mv_agenda_definition
     WHERE agenda_name = 'tournaments'
       AND segment_name = 'tournaments_main'
       AND attribute_group_name = 'tournaments_main'
       AND attribute_name = 'id_tournament'
 )
 AND id_agenda_has_agenda = (
    select  id from agenda_has_agenda where name = 'tournaments_has_competitions'
);


INSERT INTO agenda_has_agenda (name, id_agenda, id_agenda_linked, relation, is_unique, is_show_disabled_entity, is_custom_name,
                                      is_quick_add_agenda, is_quick_add_agenda_linked, is_quick_add_link, is_quick_add_link_linked, is_copyable,
                                      is_in_god_eye, is_show_graph, priority, is_tab_hidden, is_tab_hidden_linked)
VALUES ('tournaments_has_referee', (SELECT id FROM agenda WHERE name = 'tournaments'), (SELECT id FROM agenda WHERE name = 'referee'), 2, true, FALSE,
        FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 191, TRUE, true);

UPDATE attribute SET id_attribute_data_type = (SELECT id FROM attribute_data_type WHERE name = 'agenda_has_agenda'), lookup = 'tournaments_has_referee' WHERE name = 'agenda_tournament_referee';

REFRESH MATERIALIZED VIEW mv_agenda_definition;

delete from agenda_values WHERE id_agenda_definition = (SELECT id FROM flexii.mv_agenda_definition WHERE agenda_name = 'tournaments' AND attribute_name = 'agenda_tournament_referee');

-- #14056
INSERT INTO base.appuser (username, psswd, is_ldap, is_sync_exchange, is_two_factor, fullname, email, id_locale, is_system_user, enabled) VALUES ('ext_k_solution', '$2y$10$KA2r297nrSJboLW0iuE7mOUuAe/LBfWJ.DWDQjhzFluoaWtUXTCL2', false, false, false, 'Ext K-Solution', 'info@evosoft.cz', 1, true, true);
INSERT INTO base.appuser (username, psswd, is_ldap, is_sync_exchange, is_two_factor, fullname, email, id_locale, is_system_user, enabled) VALUES ('ext_mups', '$2y$10$KA2r297nrSJboLW0iuE7mOUuAe/LBfWJ.DWDQjhzFluoaWtUXTCL2', false, false, false, 'Ext MUPS', 'info@evosoft.cz', 1, true, true);
INSERT INTO base.appuser (username, psswd, is_ldap, is_sync_exchange, is_two_factor, fullname, email, id_locale, is_system_user, enabled) VALUES ('ext_esports', '$2y$10$KA2r297nrSJboLW0iuE7mOUuAe/LBfWJ.DWDQjhzFluoaWtUXTCL2', false, false, false, 'EXT eSports', 'info@evosoft.cz', 1, true, true);

INSERT INTO external_application (name, enabled, secret, id_appuser) VALUES ('ext_k_solution', true, 'EVODUMMYd7Ec59Tg4gKEQpq3ir4mjKQUwozP', (SELECT id FROM base.appuser WHERE username = 'ext_k_solution'));
INSERT INTO external_application_api_token (id_external_application, token, dt_valid_from, dt_valid_to, id_appuser_created_by, id_appuser_updated_by) VALUES ((select ID from external_application WHERE name = 'ext_k_solution'), 'EVODUMMYUHxamjQYWcxEFMRq3z37JzoLcZ7WY3iZmYyxNR9hwFYZVbvFLixYhgYK', '2023-11-24 09:33:57.000000', null, 1, 1);

INSERT INTO external_application (name, enabled, secret, id_appuser) VALUES ('ext_mups', true, 'EVODUMMYTzhhv5rALDLvW5h4hC3pxw779e4Q', (SELECT id FROM base.appuser WHERE username = 'ext_mups'));
INSERT INTO external_application_api_token (id_external_application, token, dt_valid_from, dt_valid_to, id_appuser_created_by, id_appuser_updated_by) VALUES ((select ID from external_application WHERE name = 'ext_mups'), 'EVODUMMYYvWPKZdnmwWinZkDNDmgjPTANKzCZ2FZceCwJemDe3jwchhRyJ7jrn9o', '2023-11-24 09:33:57.000000', null, 1, 1);

INSERT INTO external_application (name, enabled, secret, id_appuser) VALUES ('ext_esports', true, 'EVODUMMYXoVUP3RL7NdaAhpeFvucohpvKXHV', (SELECT id FROM base.appuser WHERE username = 'ext_esports'));
INSERT INTO external_application_api_token (id_external_application, token, dt_valid_from, dt_valid_to, id_appuser_created_by, id_appuser_updated_by) VALUES ((select ID from external_application WHERE name = 'ext_esports'), 'EVODUMMYw42MjbC3Dr9QaazVtNifKvKggbX4LiEbYTsePCQfNsFwcptvAJ2ySQep', '2023-11-24 09:33:57.000000', null, 1, 1);


-- #13599 --

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES('member_card', 'Členská karta', 0,  NULL ,NULL, 1, (select id from attribute_data_type where name = 'file'));

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

REFRESH MATERIALIZED VIEW mv_agenda_definition;
-- #13600 changes --

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



INSERT INTO configuration.configuration_group (name, is_recurring, priority) VALUES ('bookkeeping', false, 70);

INSERT INTO configuration.configuration_key (name, data_type) VALUES ('union_fee', 'string');
INSERT INTO configuration.configuration_key (name, data_type) VALUES ('name', 'string');
INSERT INTO configuration.configuration_key (name, data_type) VALUES ('company_id', 'string');
INSERT INTO configuration.configuration_key (name, data_type) VALUES ('vat', 'string');
INSERT INTO configuration.configuration_key (name, data_type) VALUES ('po_box', 'string');
INSERT INTO configuration.configuration_key (name, data_type) VALUES ('postal_address', 'string');
INSERT INTO configuration.configuration_key (name, data_type) VALUES ('city', 'string');
INSERT INTO configuration.configuration_key (name, data_type) VALUES ('bank_account', 'string');
INSERT INTO configuration.configuration_key (name, data_type) VALUES ('bank_code', '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 ('union_fee', (SELECT id FROM configuration.configuration_group WHERE name = 'bookkeeping'),
        (SELECT id FROM configuration.configuration_key WHERE name = 'union_fee'), false, '300,00', null, 'Svazový poplatek', false, null, 10);

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 ('name', (SELECT id FROM configuration.configuration_group WHERE name = 'bookkeeping'),
        (SELECT id FROM configuration.configuration_key WHERE name = 'name'), false, 'Český Svaz Juda', null, 'Název', false, null, 20);

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 ('company_id', (SELECT id FROM configuration.configuration_group WHERE name = 'bookkeeping'),
        (SELECT id FROM configuration.configuration_key WHERE name = 'company_id'), false, '00537560', null, 'IČO', false, null, 30);


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 ('vat', (SELECT id FROM configuration.configuration_group WHERE name = 'bookkeeping'),
        (SELECT id FROM configuration.configuration_key WHERE name = 'vat'), false, '200537560', null, 'DIČ', false, null, 40);


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 ('po_box', (SELECT id FROM configuration.configuration_group WHERE name = 'bookkeeping'),
        (SELECT id FROM configuration.configuration_key WHERE name = 'po_box'), false, '40', null, 'P.O Box', false, null, 60);

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 ('postal_address', (SELECT id FROM configuration.configuration_group WHERE name = 'bookkeeping'),
        (SELECT id FROM configuration.configuration_key WHERE name = 'postal_address'), false, '160 17', null, 'PSČ', false, null, 70);

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 ('city', (SELECT id FROM configuration.configuration_group WHERE name = 'bookkeeping'),
        (SELECT id FROM configuration.configuration_key WHERE name = 'city'), false, 'Praha 6 - Strahov', null, 'Město', false, null, 80);

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 ('bank_account', (SELECT id FROM configuration.configuration_group WHERE name = 'bookkeeping'),
        (SELECT id FROM configuration.configuration_key WHERE name = 'bank_account'), false, '5080998001', null, 'Číslo bankovního účtu', false, null, 90);

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 ('bank_code', (SELECT id FROM configuration.configuration_group WHERE name = 'bookkeeping'),
        (SELECT id FROM configuration.configuration_key WHERE name = 'bank_code'), false, '5500', null, 'Kód banky', false, null, 100);

delete from agenda_values
WHERE id_agenda_definition in (
    SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
      AND segment_name = 'members_fee'
      AND attribute_group_name = 'members_fee'
      AND attribute_name in ('member_fee', 'member_fee_per_year', 'member_fee_order_date')
);

delete from agenda_definition
WHERE id = (
    SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
      AND segment_name = 'members_fee'
      AND attribute_group_name = 'members_fee'
      AND attribute_name = 'member_fee_order_date'
);

delete from agenda_definition
WHERE id = (
    SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
      AND segment_name = 'members_fee'
      AND attribute_group_name = 'members_fee'
      AND attribute_name = 'member_fee_per_year'
);
delete from agenda_definition
WHERE id = (
    SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
      AND segment_name = 'members_fee'
      AND attribute_group_name = 'members_fee'
      AND attribute_name = 'member_fee'
);

delete from agenda_has_segment_has_attribute_group_definition
where id_agenda_has_segment_definition = (
    select id from agenda_has_segment_definition
    where id_agenda = (select id from agenda where name = 'members')
      and id_agenda_segment = (select id from agenda_segment where name = 'members_fee')
)
  AND id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'members_fee');

-------
delete from agenda_has_agenda_values av
where id_agenda_has_agenda_definition  in (
    select id
    FROM agenda_has_agenda_definition
    WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
      and id_attribute = (select id from attribute where name = 'members_fee')
) and value is null;

DELETE
FROM agenda_has_segment_definition
WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'members')
  AND id_agenda_segment = (SELECT id from agenda_segment WHERE name = 'members_fee');

delete from agenda_has_agenda_values av
where id_agenda_has_agenda_definition  in (
    select id
    FROM agenda_has_agenda_definition
    WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
      and id_attribute = (select id from attribute where name = 'clubs_has_union_fees')
) and value is null;

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

delete from agenda_has_agenda_values av
where id_agenda_has_agenda_definition  in (
    select id
    FROM agenda_has_agenda_definition
    WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
      and id_attribute = (select id from attribute where name = 'members_number')
) and value is null;

DELETE
FROM agenda_has_agenda_definition
WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
  and id_attribute = (select id from attribute where name = 'members_number');

delete from agenda_has_agenda_values av
where id_agenda_has_agenda_definition  in (
    select id
    FROM agenda_has_agenda_definition
    WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
      and id_attribute = (select id from attribute where name = 'variable_symbol')
) and value is null;

DELETE
FROM agenda_has_agenda_definition
WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
  and id_attribute = (select id from attribute where name = 'variable_symbol');

delete from agenda_has_agenda_values av
where id_agenda_has_agenda_definition  in (
    select id
    FROM agenda_has_agenda_definition
    WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
      and id_attribute = (select id from attribute where name = 'total_amount')
) and value is null;

DELETE
FROM agenda_has_agenda_definition
WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
  and id_attribute = (select id from attribute where name = 'total_amount');

delete from agenda_has_agenda_values av
where id_agenda_has_agenda_definition  in (
    select id
    FROM agenda_has_agenda_definition
    WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
      and id_attribute = (select id from attribute where name = 'dt_of_payment_issue')
) and value is null;

DELETE
FROM agenda_has_agenda_definition
WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
  and id_attribute = (select id from attribute where name = 'dt_of_payment_issue');

delete from agenda_has_agenda_values av
where id_agenda_has_agenda_definition  in (
    select id
    FROM agenda_has_agenda_definition
    WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
      and id_attribute = (select id from attribute where name = 'year_fee')
) and value is null;

DELETE
FROM agenda_has_agenda_definition
WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
  and id_attribute = (select id from attribute where name = 'year_fee');

delete from agenda_has_agenda_values av
where id_agenda_has_agenda_definition  in (
    select id
    FROM agenda_has_agenda_definition
    WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
      and id_attribute = (select id from attribute where name = 'state_of_payment')
) and value is null;

DELETE
FROM agenda_has_agenda_definition
WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
  and id_attribute = (select id from attribute where name = 'state_of_payment');

delete from agenda_has_agenda_values av
where id_agenda_has_agenda_definition  in (
    select id
    FROM agenda_has_agenda_definition
    WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
      and id_attribute = (select id from attribute where name = 'proforma')
) and value is null;

DELETE
FROM agenda_has_agenda_definition
WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
  and id_attribute = (select id from attribute where name = 'proforma');

delete from agenda_has_agenda_values av
where id_agenda_has_agenda_definition  in (
    select id
    FROM agenda_has_agenda_definition
    WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
      and id_attribute = (select id from attribute where name = 'invoice')
) and value is null;

DELETE
FROM agenda_has_agenda_definition
WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
  and id_attribute = (select id from attribute where name = 'invoice');
----

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_union_fees')
       , (select id
          from agenda_definition
          where id_attribute = (select id from attribute where name = 'clubs_has_union_fees')
            AND id_agenda_segment_attribute_group_definition =
                (select id
                 from agenda_has_segment_has_attribute_group_definition
                 where id_agenda_has_segment_definition =
                       (select id
                        from agenda_has_segment_definition
                        where id_agenda = (select id from agenda where name = 'union_fees')
                          and id_agenda_segment =
                              (select id from agenda_segment where name = 'union_fees_main'))
                   and id_agenda_attribute_group =
                       (select id from agenda_attribute_group where name = 'union_fees_main')))
       , false);

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_union_fees')
       , (select id
          from agenda_definition
          where id_attribute = (select id from attribute where name = 'members_number')
            AND id_agenda_segment_attribute_group_definition =
                (select id
                 from agenda_has_segment_has_attribute_group_definition
                 where id_agenda_has_segment_definition =
                       (select id
                        from agenda_has_segment_definition
                        where id_agenda = (select id from agenda where name = 'union_fees')
                          and id_agenda_segment =
                              (select id from agenda_segment where name = 'union_fees_main'))
                   and id_agenda_attribute_group =
                       (select id from agenda_attribute_group where name = 'union_fees_main')))
       , false);


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_union_fees')
       , (select id
          from agenda_definition
          where id_attribute = (select id from attribute where name = 'variable_symbol')
            AND id_agenda_segment_attribute_group_definition =
                (select id
                 from agenda_has_segment_has_attribute_group_definition
                 where id_agenda_has_segment_definition =
                       (select id
                        from agenda_has_segment_definition
                        where id_agenda = (select id from agenda where name = 'union_fees')
                          and id_agenda_segment =
                              (select id from agenda_segment where name = 'union_fees_main'))
                   and id_agenda_attribute_group =
                       (select id from agenda_attribute_group where name = 'union_fees_main')))
       , false);


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_union_fees')
       , (select id
          from agenda_definition
          where id_attribute = (select id from attribute where name = 'total_amount')
            AND id_agenda_segment_attribute_group_definition =
                (select id
                 from agenda_has_segment_has_attribute_group_definition
                 where id_agenda_has_segment_definition =
                       (select id
                        from agenda_has_segment_definition
                        where id_agenda = (select id from agenda where name = 'union_fees')
                          and id_agenda_segment =
                              (select id from agenda_segment where name = 'union_fees_main'))
                   and id_agenda_attribute_group =
                       (select id from agenda_attribute_group where name = 'union_fees_main')))
       , false);


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_union_fees')
       , (select id
          from agenda_definition
          where id_attribute = (select id from attribute where name = 'dt_of_payment_issue')
            AND id_agenda_segment_attribute_group_definition =
                (select id
                 from agenda_has_segment_has_attribute_group_definition
                 where id_agenda_has_segment_definition =
                       (select id
                        from agenda_has_segment_definition
                        where id_agenda = (select id from agenda where name = 'union_fees')
                          and id_agenda_segment =
                              (select id from agenda_segment where name = 'union_fees_main'))
                   and id_agenda_attribute_group =
                       (select id from agenda_attribute_group where name = 'union_fees_main')))
       , false);


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_union_fees')
       , (select id
          from agenda_definition
          where id_attribute = (select id from attribute where name = 'year_fee')
            AND id_agenda_segment_attribute_group_definition =
                (select id
                 from agenda_has_segment_has_attribute_group_definition
                 where id_agenda_has_segment_definition =
                       (select id
                        from agenda_has_segment_definition
                        where id_agenda = (select id from agenda where name = 'union_fees')
                          and id_agenda_segment =
                              (select id from agenda_segment where name = 'union_fees_main'))
                   and id_agenda_attribute_group =
                       (select id from agenda_attribute_group where name = 'union_fees_main')))
       , false);


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_union_fees')
       , (select id
          from agenda_definition
          where id_attribute = (select id from attribute where name = 'state_of_payment')
            AND id_agenda_segment_attribute_group_definition =
                (select id
                 from agenda_has_segment_has_attribute_group_definition
                 where id_agenda_has_segment_definition =
                       (select id
                        from agenda_has_segment_definition
                        where id_agenda = (select id from agenda where name = 'union_fees')
                          and id_agenda_segment =
                              (select id from agenda_segment where name = 'union_fees_main'))
                   and id_agenda_attribute_group =
                       (select id from agenda_attribute_group where name = 'union_fees_main')))
       , false);


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_union_fees')
       , (select id
          from agenda_definition
          where id_attribute = (select id from attribute where name = 'proforma')
            AND id_agenda_segment_attribute_group_definition =
                (select id
                 from agenda_has_segment_has_attribute_group_definition
                 where id_agenda_has_segment_definition =
                       (select id
                        from agenda_has_segment_definition
                        where id_agenda = (select id from agenda where name = 'union_fees')
                          and id_agenda_segment =
                              (select id from agenda_segment where name = 'union_fees_main'))
                   and id_agenda_attribute_group =
                       (select id from agenda_attribute_group where name = 'union_fees_main')))
       , false);

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_union_fees')
       , (select id
          from agenda_definition
          where id_attribute = (select id from attribute where name = 'invoice')
            AND id_agenda_segment_attribute_group_definition =
                (select id
                 from agenda_has_segment_has_attribute_group_definition
                 where id_agenda_has_segment_definition =
                       (select id
                        from agenda_has_segment_definition
                        where id_agenda = (select id from agenda where name = 'union_fees')
                          and id_agenda_segment =
                              (select id from agenda_segment where name = 'union_fees_main'))
                   and id_agenda_attribute_group =
                       (select id from agenda_attribute_group where name = 'union_fees_main')))
       , false);



INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_member_fee', 'false', 'Ne', 10);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_member_fee', 'true', 'Ano', 20);
INSERT INTO MASTER_LOOKUP (LOOKUP_NAME, NAME, DESCRIPTION, PRIORITY) VALUES('lookup_member_fee', 'waiting', 'Zažádáno', 30);


UPDATE attribute
SET lookup = 'lookup_member_fee',
    id_attribute_data_type = (select id from attribute_data_type where name = 'lookup')
WHERE name = 'member_fee_current_year';


UPDATE agenda_definition
SET default_value = (select id from master_lookup where name = 'false' and lookup_name = 'lookup_member_fee'),
    id_attribute = (select id from attribute where name = 'member_fee_current_year')
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 = 'member_fee_current_year');

/*TODO STARTS HERE */

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


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 = 'union_fees')
             and id_agenda_segment = (select id from agenda_segment where name = 'union_fees_main')
          )
      , (select id from agenda_attribute_group where name = 'union_fees_autoinfo')
      , false, 2
      );


INSERT INTO agenda_definition (id_attribute, priority, is_not_null, default_value, is_disabled
                              , is_read_only, tooltip, field_column_length, skip_column_length, field_column_height, is_in_datagrid, is_search_by, in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition)
VALUES(
          (select id from attribute where name = 'name')
      , 5, false, NULL, false, true, NULL, 3, 0, 1, true, true, 4, 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 = 'union_fees')
               and id_agenda_segment = (select id from agenda_segment where name = 'union_fees_main')
         )
           and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'union_fees_autoinfo')
          )
      );

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_union_fees_agenda_sync'
       , (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_union_fees')
       , null
       , NULL
       , true
       , false
       , false
       , (select id FROM agenda WHERE name = 'clubs')
       );
refresh materialized view mv_agenda_definition;


INSERT INTO agenda_definition_to_agenda_definition_mapping
(name, id_agenda_definition, id_agenda_definition_linked)
VALUES('clubs_has_union_fees_agenda_sync-club_name'
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'clubs'
           AND segment_name = 'clubs_main'
           AND attribute_group_name = 'clubs_main'
           AND attribute_name = 'name'
       )
      , (SELECT id FROM mv_agenda_definition
         WHERE agenda_name = 'union_fees'
           AND segment_name = 'union_fees_main'
           AND attribute_group_name = 'union_fees_autoinfo'
           AND attribute_name = 'name'
       )
      );


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_union_fees_agenda_sync')
       , (SELECT id FROM agenda_definition_to_agenda_definition_mapping WHERE name = 'clubs_has_union_fees_agenda_sync-club_name')
       );

UPDATE agenda_definition
SET in_title_and_priority = null
WHERE id = ( SELECT id
             FROM mv_agenda_definition
             WHERE attribute_name = 'union_fees'
               AND attribute_group_name = 'union_fees_main'
               AND segment_name = 'union_fees_main'
               AND agenda_name = 'clubs_has_union_fees'
);

UPDATE agenda
SET is_creatable = false
WHERE name = 'union_fees';

delete from agenda_has_agenda_values av
where id_agenda_has_agenda_definition  in (
    select id
    FROM agenda_has_agenda_definition
    WHERE id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_members')
      and id_attribute = (select id from attribute where name = 'member_fee_current_year')
) and value is null or value = '300';

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


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 agenda_definition
          where id_attribute = (select id from attribute where name = 'member_fee_current_year')
            AND id_agenda_segment_attribute_group_definition = (select id
                                                                from agenda_has_segment_has_attribute_group_definition
                                                                where id_agenda_has_segment_definition = (select id
                                                                                                          from agenda_has_segment_definition
                                                                                                          where id_agenda = (select id from agenda where name = 'members')
                                                                                                            and id_agenda_segment =
                                                                                                                (select id from agenda_segment where name = 'members_main'))
                                                                  and id_agenda_attribute_group =
                                                                      (select id from agenda_attribute_group where name = 'members_main')))
       , false);



UPDATE agenda_button
SET icon = 'fal fa-money-bill-wave'
WHERE name = 'UnionFeePayment';

UPDATE agenda_button
SET icon = 'fal fa-ban'
WHERE name = 'UnionFeeCancel';

UPDATE agenda
SET is_deletable = false
WHERE name = 'union_fees';


INSERT INTO agenda_has_agenda_show_attribute (id_agenda_has_agenda, id_agenda_definition, is_hidden_in_datagrid)
VALUES ( (select id from agenda_has_agenda where name = 'members_has_union_fees')
       , (select id
          from agenda_definition
          where id_attribute = (select id from attribute where name = 'state_of_payment')
            AND id_agenda_segment_attribute_group_definition = (select id
                                                                from agenda_has_segment_has_attribute_group_definition
                                                                where id_agenda_has_segment_definition = (select id
                                                                                                          from agenda_has_segment_definition
                                                                                                          where id_agenda = (select id from agenda where name = 'union_fees')
                                                                                                            and id_agenda_segment =
                                                                                                                (select id from agenda_segment where name = 'union_fees_main'))
                                                                  and id_agenda_attribute_group =
                                                                      (select id from agenda_attribute_group where name = 'union_fees_main')))
       , false);



UPDATE agenda_has_agenda
SET is_editable_agenda = false,
    is_editable_agenda_linked = false,
    is_addable = false,
    is_addable_linked = false
WHERE name = 'members_has_union_fees';


INSERT INTO configuration.configuration_key (name, data_type)
VALUES ('street', '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 ('street', (SELECT id FROM configuration.configuration_group WHERE name = 'bookkeeping'),
        (SELECT id FROM configuration.configuration_key WHERE name = 'street'), false, 'Zátopkova 100/2', null,
        'Ulice', false, null, 50);


UPDATE attribute
    SET id_attribute_data_type = (select id from attribute_data_type where name = 'string')
WHERE name = 'total_amount';


REFRESH MATERIALIZED VIEW mv_agenda_definition;

UPDATE attribute SET custom_data_class = 'RefereeId' WHERE name = 'id_referee';
-- #13938
insert into configuration.configuration_group (name, is_recurring, priority)
    select 'csju_mssql_db_snapshot_server', false, 100
    where not exists(
        select id from configuration.configuration_group where name = 'csju_mssql_db_snapshot_server'
);

insert into configuration.configuration_key (name, data_type)
    select 'csju_mssql_allowed', 'bool' union all
    select 'csju_mssql_host', 'string' union all
    select 'csju_mssql_port', 'string' union all
    select 'csju_mssql_database', 'string' union all
    select 'csju_mssql_user', 'string' union all
    select 'csju_mssql_password', 'password'
    where not exists(
        select id from configuration.configuration_key where name in ('csju_mssql_allowed', 'csju_mssql_host', 'csju_mssql_port', 'csju_mssql_database', 'csju_mssql_user', 'csju_mssql_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)
    select 'csju_mssql_allowed', (select id from configuration.configuration_group where name = 'csju_mssql_db_snapshot_server'), (select id from configuration.configuration_key where name = 'csju_mssql_allowed'), true, null, '', '', false, null::smallint, 100 union all
    select 'csju_mssql_host', (select id from configuration.configuration_group where name = 'csju_mssql_db_snapshot_server'), (select id from configuration.configuration_key where name = 'csju_mssql_host'), true, null, '', '', false, null::smallint, 110 union all
    select 'csju_mssql_port', (select id from configuration.configuration_group where name = 'csju_mssql_db_snapshot_server'), (select id from configuration.configuration_key where name = 'csju_mssql_port'), true, null, '', '', false, null::smallint, 120 union all
    select 'csju_mssql_database', (select id from configuration.configuration_group where name = 'csju_mssql_db_snapshot_server'), (select id from configuration.configuration_key where name = 'csju_mssql_database'), true, null, '', '', false, null::smallint, 130 union all
    select 'csju_mssql_user', (select id from configuration.configuration_group where name = 'csju_mssql_db_snapshot_server'), (select id from configuration.configuration_key where name = 'csju_mssql_user'), true, null, '', '', false, null::smallint, 140 union all
    select 'csju_mssql_password', (select id from configuration.configuration_group where name = 'csju_mssql_db_snapshot_server'), (select id from configuration.configuration_key where name = 'csju_mssql_password'), true, null, '', '', false, null::smallint, 150
    where not exists(
        select id from configuration.configuration_definition where name in ('csju_mssql_allowed', 'csju_mssql_host', 'csju_mssql_port', 'csju_mssql_database', 'csju_mssql_user', 'csju_mssql_password')
);

insert into configuration.configuration_values (id_configuration_definition, value)
    select (select id from configuration.configuration_definition where name = 'csju_mssql_allowed'), 'true' union all
    select (select id from configuration.configuration_definition where name = 'csju_mssql_host'), '192.168.88.234' union all
    select (select id from configuration.configuration_definition where name = 'csju_mssql_port'), '1433' union all
    select (select id from configuration.configuration_definition where name = 'csju_mssql_database'), 'JuEvidence' union all
    select (select id from configuration.configuration_definition where name = 'csju_mssql_user'), 'SA' union all
    select (select id from configuration.configuration_definition where name = 'csju_mssql_password'), 'SXJlYnpVVWx5a1cyNERUeGR5Zko3dz09'
    where not exists(
        select id from configuration.configuration_values where id_configuration_definition in (
            select id from configuration.configuration_definition where name in ('csju_mssql_allowed', 'csju_mssql_host', 'csju_mssql_port', 'csju_mssql_database', 'csju_mssql_user', 'csju_mssql_password')
        )
);

update flexii.agenda_has_external_source
set enabled = false
where name in ('JudoMembers','JudoParents','JudoClubs','JudoTournaments');

update attribute
set lookup = 'lookup_member_fee', id_attribute_data_type = (select id from attribute_data_type where name = 'lookup')
where name = 'member_fee_current_year';

-- insert into master_lookup (lookup_name, name, description, priority)
--     select 'lookup_member_fee', 'false', 'Ne', 10 union all
--     select 'lookup_member_fee', 'true', 'Ano', 20 union all
--     select 'lookup_member_fee', 'waiting', 'Zažádáno', 30
-- where not exists(
--     select id from master_lookup where lookup_name = 'lookup_member_fee'
-- );

-- #13938 -- fix #13600
update agenda_definition
set is_unique = false
where id in (
    select id
    from mv_agenda_definition
    where 1 = 1
    and agenda_name = 'union_fees'
    and attribute_group_name = 'union_fees_main'
);

insert into agenda_values (id_agenda_entity, id_agenda_definition, recurring_sequence, recurring_sequence_interlaced, value, id_external_application_created_by, is_anonymized, dt_anonymized, id_appuser_anonymized_by, id_appuser_created_by, id_appuser_updated_by)
select
    ae.id as id_agenda_entity
    , (select id from mv_agenda_definition where agenda_name = 'union_fees' and attribute_group_name = 'union_fees_autoinfo' and attribute_name = 'name') as id_agenda_definition
    , null as recurring_sequence
    , null as recurring_sequence_interlaced
    , aec.value as value
    , null as id_external_application_created_by
    , false as is_anonymized
    , null as dt_anonymized
    , null as id_appuser_anonymized_by
    , (select id from base.appuser where username = 'judo_internal') as id_appuser_created_by
    , (select id from base.appuser where username = 'judo_internal') as id_appuser_updated_by
from agenda_entity ae
join agenda_entity_has_agenda_entity aehae on aehae.id_agenda_entity_linked = ae.id and aehae.id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'clubs_has_union_fees')
join agenda_values aec on aec.id_agenda_entity = aehae.id_agenda_entity and aec.id_agenda_definition = (select id from mv_agenda_definition where agenda_name = 'clubs' and attribute_group_name = 'clubs_main' and attribute_name = 'name')
where ae.id_agenda = (select id from agenda where name = 'union_fees')
and ae.enabled is true
and aehae.enabled is true
and ae.id not in (
    select xae.id
    from agenda_entity xae
    join agenda_values xav on xae.id = xav.id_agenda_entity and xav.id_agenda_definition = (select id from mv_agenda_definition where agenda_name = 'union_fees' and attribute_group_name = 'union_fees_autoinfo' and attribute_name = 'name')
    where xae.id_agenda = (select id from agenda where name = 'union_fees')
    and xae.enabled is true
    and xav.enabled is true
);


update agenda_definition
set is_not_null = true
where id in (
    select id
    from mv_agenda_definition
    where 1 = 1
      and agenda_name = 'tournament_administrator'
      and attribute_group_name = 'tournament_administrator_main'
      and segment_name = 'tournament_administrator_main'
      and attribute_name = 'appuser'
);


update agenda_definition
set is_not_null = true
where id in (
    select id
    from mv_agenda_definition
    where 1 = 1
      and agenda_name = 'tournament_administrator'
      and attribute_group_name = 'tournament_administrator_main'
      and segment_name = 'tournament_administrator_main'
      and attribute_name = 'members_has_tournament_administrator'
);

update agenda_definition
set is_unique = true
where id in (select id
             from mv_agenda_definition
             where 1 = 1
               and agenda_name = 'members'
               and attribute_group_name = 'members_main'
               and segment_name = 'members_main'
               and attribute_name = 'identifier_value');

update agenda_definition
set is_unique = true
where id in (select id
             from mv_agenda_definition
             where 1 = 1
               and agenda_name = 'members'
               and attribute_group_name = 'members_main'
               and segment_name = 'members_main'
               and attribute_name = 'identifier_value');


update agenda_definition
set is_disabled = true
where id in (select id
             from mv_agenda_definition
             where 1 = 1
               and agenda_name = 'union_fees'
               and attribute_group_name = 'union_fees_main'
               and segment_name = 'union_fees_main'
               and attribute_name = 'proforma');




-- #13599
UPDATE master_lookup SET name = 'planned' WHERE lookup_name = 'lookup_tournament_status' AND name = 'status_1';
UPDATE master_lookup SET name = 'started' WHERE lookup_name = 'lookup_tournament_status' AND name = 'status_2';
UPDATE master_lookup SET name = 'closed' WHERE lookup_name = 'lookup_tournament_status' AND name = 'status_3';
UPDATE master_lookup SET name = 'canceled' WHERE lookup_name = 'lookup_tournament_status' AND name = 'status_4';

UPDATE agenda_definition SET default_value = (SELECT id FROM master_lookup WHERE enabled = true AND lookup_name = 'lookup_tournament_status' AND name = 'planned')
WHERE id = (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournaments' AND segment_name = 'tournaments_main' AND attribute_group_name = 'tournaments_main' AND attribute_name = 'tournament_status');

-- #13841
INSERT INTO agenda_button (id_agenda, name, description, icon, class)
VALUES ((SELECT id FROM agenda WHERE name = 'tournaments'), 'TournamentCancel', 'Zruš turnaj', 'fal fa-ban', 'btn-danger');

INSERT INTO agenda_action (name, description, class_name, is_via_rabbit)
VALUES ('TournamentCancel', 'Zruš turnaj', 'TournamentCancelAction', true);

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


REFRESH MATERIALIZED VIEW mv_agenda_definition;


--
INSERT INTO flexii.custom_import_type (id_agenda, name, description) VALUES ((SELECT id FROM agenda WHERE name = 'members'), 'MembersToClubImport', 'Import členů do klubu');


-- #14021
update agenda_has_agenda
set
    is_addable = false
  , is_addable_linked = false
  , is_editable_agenda = true
  , is_editable_agenda_linked = true
  , is_deletable = true
  , is_deletable_linked = true
where name = 'tournaments_has_members';

alter table lookup_competition_type add column if not exists description varchar(200) default null;
update lookup_competition_type set description = (initcap(coalesce(cat1, '')) || ' - ' || initcap(coalesce(cat2, '')) || ' - ' || initcap(coalesce(cat3, '')) || ' - '  || initcap(coalesce(cat4, '')) || ' - '  || initcap(coalesce(cat5, ''))) where 1 = 1;
alter table lookup_competition_type alter column description set not null;


-- remove member card --
delete from agenda_values
WHERE id_agenda_definition in (
    SELECT id FROM mv_agenda_definition
    WHERE agenda_name = 'members'
      AND segment_name = 'members_main'
      AND attribute_group_name = 'members_main'
      AND attribute_name = 'member_card'
);

delete from agenda_definition
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 = 'member_card'
);

delete from attribute where name = 'member_card';

REFRESH MATERIALIZED VIEW mv_agenda_definition;

----------------------------------------------------
-- 2.6.3.0
-- SQL for this version ends here
----------------------------------------------------
UPDATE flexii.lookup_locations SET name = 'region_3' WHERE name = 'Jihočeský kraj';
UPDATE flexii.lookup_locations SET name = 'region_4' WHERE name = 'Plzeňský kraj';
UPDATE flexii.lookup_locations SET name = 'region_5' WHERE name = 'Karlovarský kraj';
UPDATE flexii.lookup_locations SET name = 'region_6' WHERE name = 'Ústecký kraj';
UPDATE flexii.lookup_locations SET name = 'region_2' WHERE name = 'Středočeský kraj';
UPDATE flexii.lookup_locations SET name = 'region_1' WHERE name = 'Praha';
UPDATE flexii.lookup_locations SET name = 'region_10' WHERE name = 'Kraj Vysočina';
UPDATE flexii.lookup_locations SET name = 'region_7' WHERE name = 'Liberecký kraj';
UPDATE flexii.lookup_locations SET name = 'region_8' WHERE name = 'Královéhradecký kraj';
UPDATE flexii.lookup_locations SET name = 'region_9' WHERE name = 'Pardubický kraj';
UPDATE flexii.lookup_locations SET name = 'region_11' WHERE name = 'Jihomoravský kraj';
UPDATE flexii.lookup_locations SET name = 'region_12' WHERE name = 'Olomoucký kraj';
UPDATE flexii.lookup_locations SET name = 'region_13' WHERE name = 'Zlínský kraj';
UPDATE flexii.lookup_locations SET name = 'region_14' WHERE name = 'Moravskoslezský kraj';
UPDATE flexii.lookup_locations SET name = 'cr' WHERE name = 'Česká republika';

UPDATE flexii.attribute SET lookup_columns = '{"id":"id","name":"description"}' WHERE name = 'locations';

--------------------------
-- #13634 - permissions --

UPDATE user_group_has_agenda SET is_can_create = is_can_write WHERE is_can_create IS NULL;
UPDATE user_group_has_agenda SET is_can_delete = is_can_write WHERE is_can_delete IS NULL;

-- groups creation
INSERT INTO base.user_group (description, name) VALUES
                                                    ('Členové - vše - čtení', 'Clenove_Vse_Cteni'),
                                                    ('Členové - Jihočeský kraj - čtení + edit vyznamenání kraj', 'Clenove_JihoceskyKraj_Cteni+EditVyznamenaniKraj'),
                                                    ('Členové - Jihomoravský kraj - čtení + edit vyznamenání kraj', 'Clenove_JihomoravskyKraj_Cteni+EditVyznamenaniKraj'),
                                                    ('Členové - Karlovarský kraj - čtení + edit vyznamenání kraj', 'Clenove_KarlovarskyKraj_Cteni+EditVyznamenaniKraj'),
                                                    ('Členové - Královéhradecký kraj - čtení + edit vyznamenání kraj', 'Clenove_KralovehradeckyKraj_Cteni+EditVyznamenaniKraj'),
                                                    ('Členové - Liberecký kraj - čtení + edit vyznamenání kraj', 'Clenove_LibereckyKraj_Cteni+EditVyznamenaniKraj'),
                                                    ('Členové - Moravskoslezský kraj - čtení + edit vyznamenání kraj', 'Clenove_MoravskoslezskyKraj_Cteni+EditVyznamenaniKraj'),
                                                    ('Členové - Olomoucký kraj - čtení + edit vyznamenání kraj', 'Clenove_OlomouckyKraj_Cteni+EditVyznamenaniKraj'),
                                                    ('Členové - Pardubický kraj - čtení + edit vyznamenání kraj', 'Clenove_PardubickyKraj_Cteni+EditVyznamenaniKraj'),
                                                    ('Členové - Plzeňský kraj - čtení + edit vyznamenání kraj', 'Clenove_PlzenskyKraj_Cteni+EditVyznamenaniKraj'),
                                                    ('Členové - Praha - čtení + edit vyznamenání kraj', 'Clenove_Praha_Cteni+EditVyznamenaniKraj'),
                                                    ('Členové - Středočeský kraj - čtení + edit vyznamenání kraj', 'Clenove_StredoceskyKraj_Cteni+EditVyznamenaniKraj'),
                                                    ('Členové - Ústecký kraj - čtení + edit vyznamenání kraj', 'Clenove_UsteckyKraj_Cteni+EditVyznamenaniKraj'),
                                                    ('Členové - Vysočina - čtení + edit vyznamenání kraj', 'Clenove_Vysocina_Cteni+EditVyznamenaniKraj'),
                                                    ('Členové - Zlínský kraj - čtení + edit vyznamenání kraj', 'Clenove_ZlinskyKraj_Cteni+EditVyznamenaniKraj'),
                                                    ('Členové - vše - edit + přidání', 'Clenove_Vse_Edit+Pridani'),
                                                    ('Členové - členové klubu admina - edit limitovan + přidání', 'Clenove_ClenoveKlubuAdmina_EditLimitovan+Pridani'),
                                                    ('Členové - vše - editace vyznamenani kraj a svaz', 'Clenove_Vse_EditaceVyznamenaniKrajASvaz'),
                                                    ('Členové - vše - přidání DAN', 'Clenove_Vse_PridaniDan'),
                                                    ('Zákonný zástupce - vše - čtení', 'ZakonnyZastupce_Vse_Cteni'),
                                                    ('Zákonný zástupce - vše - edit + přidání', 'ZakonnyZastupce_Vse_Edit+Pridani'),
                                                    ('Svazové poplatky - vše - čtení', 'ClenskePoplatky_Vse_Cteni'),
                                                    ('Svazové poplatky - vše - editace faktury', 'ClenskePoplatky_Vse_EditaceFaktury'),
                                                    ('Svazové poplatky - vše - zaplaceno + zrušit', 'ClenskePoplatky_Vse_Zaplaceno+Zrusit'),
                                                    ('Kluby - vše - čtení', 'Kluby_Vse_Cteni'),
                                                    ('Kluby - vše - edit + přidání + tisk karet', 'Kluby_Vse_Edit+Pridani+TiskKaret'),
                                                    ('Kluby - adminem klubu - edit limitovan + objednani poplatku + tisk karet', 'Kluby_AdminemKlubu_EditLimitovan+ObjednaniPoplatku+TiskKaret'),
                                                    ('Administrátor klubu - vše - čtení', 'AdministratorKlubu_Vse_Cteni'),
                                                    ('Administrátor klubu - vše - edit + přidání', 'AdministratorKlubu_Vse_Edit+Pridani'),
                                                    ('Turnaje - vše - čtení', 'Turnaje_Vse_Cteni'),
                                                    ('Turnaje - hlavním rozhodčím - čtení', 'Turnaje_HlavnimRozhodcim_Cteni'),
                                                    ('Turnaje - vše - edit + přidání', 'Turnaje_Vse_Edit+Pridani'),
                                                    ('Turnaje - turnaj aktivni a pořádán klubem Admina - čtení + přihlášení členů svého klubu', 'Turnaje_TurnajAktivniAPoradanKlubemAdmina_Cteni+PrihlaseniClenuSvehoKlubu'),
                                                    ('Turnaje - adminem turnaje - edit + spuštění + tisk hesel', 'Turnaje_AdminemTurnaje_Edit+Spusteni+TiskHesel'),
                                                    ('Turnaje - Jihočeský kraj - edit + přidání', 'Turnaje_JihoceskyKraj_Edit+Pridani'),
                                                    ('Turnaje - Jihomoravský kraj - edit + přidání', 'Turnaje_JihomoravskyKraj_Edit+Pridani'),
                                                    ('Turnaje - Karlovarský kraj - edit + přidání', 'Turnaje_KarlovarskyKraj_Edit+Pridani'),
                                                    ('Turnaje - Královéhradecký kraj - edit + přidání', 'Turnaje_KralovehradeckyKraj_Edit+Pridani'),
                                                    ('Turnaje - Liberecký kraj - edit + přidání', 'Turnaje_LibereckyKraj_Edit+Pridani'),
                                                    ('Turnaje - Moravskoslezský kraj - edit + přidání', 'Turnaje_MoravskoslezskyKraj_Edit+Pridani'),
                                                    ('Turnaje - Olomoucký kraj - edit + přidání', 'Turnaje_OlomouckyKraj_Edit+Pridani'),
                                                    ('Turnaje - Pardubický kraj - edit + přidání', 'Turnaje_PardubickyKraj_Edit+Pridani'),
                                                    ('Turnaje - Plzeňský kraj - edit + přidání', 'Turnaje_PlzenskyKraj_Edit+Pridani'),
                                                    ('Turnaje - Praha - edit + přidání', 'Turnaje_Praha_Edit+Pridani'),
                                                    ('Turnaje - Středočeský kraj - edit + přidání', 'Turnaje_StredoceskyKraj_Edit+Pridani'),
                                                    ('Turnaje - Ústecký kraj - edit + přidání', 'Turnaje_UsteckyKraj_Edit+Pridani'),
                                                    ('Turnaje - Vysočina - edit + přidání', 'Turnaje_Vysocina_Edit+Pridani'),
                                                    ('Turnaje - Zlínský kraj - edit + přidání', 'Turnaje_ZlinskyKraj_Edit+Pridani'),
                                                    ('Administrátor turnaje - vše - čtení', 'AdministratorTurnaje_Vse_Cteni'),
                                                    ('Administrátor turnaje - vše - edit + přidání', 'AdministratorTurnaje_Vse_Edit+Pridani'),
                                                    ('Soutěže dlouhodobé - vše - čtení', 'SoutezeDlouhodobe_Vse_Cteni'),
                                                    ('Soutěže dlouhodobé - vše - edit + přidání', 'SoutezeDlouhodobe_Vse_Edit+Pridani'),
                                                    ('Rozhodčí - vše - čtení', 'Rozhodci_Vse_Cteni'),
                                                    ('Rozhodčí - vše - edit + přidání', 'Rozhodci_Vse_Edit+Pridani'),
                                                    ('Rozhodčí - vše - edit limitovan', 'Rozhodci_Vse_EditLimitovan'),
                                                    ('Rozhodčí - člen klubu Jihočeský kraj - edit 3. třída', 'Rozhodci_ClenKlubuJihoceskyKraj_Edit3.Trida'),
                                                    ('Rozhodčí - člen klubu Jihomoravský kraj - edit 3. třída', 'Rozhodci_ClenKlubuJihomoravskyKraj_Edit3.Trida'),
                                                    ('Rozhodčí - člen klubu Karlovarský kraj - edit 3. třída', 'Rozhodci_ClenKlubuKarlovarskyKraj_Edit3.Trida'),
                                                    ('Rozhodčí - člen klubu Královéhradecký kraj - edit 3. třída', 'Rozhodci_ClenKlubuKralovehradeckyKraj_Edit3.Trida'),
                                                    ('Rozhodčí - člen klubu Liberecký kraj - edit 3. třída', 'Rozhodci_ClenKlubuLibereckyKraj_Edit3.Trida'),
                                                    ('Rozhodčí - člen klubu Moravskoslezský kraj - edit 3. třída', 'Rozhodci_ClenKlubuMoravskoslezskyKraj_Edit3.Trida'),
                                                    ('Rozhodčí - člen klubu Olomoucký kraj - edit 3. třída', 'Rozhodci_ClenKlubuOlomouckyKraj_Edit3.Trida'),
                                                    ('Rozhodčí - člen klubu Pardubický kraj - edit 3. třída', 'Rozhodci_ClenKlubuPardubickyKraj_Edit3.Trida'),
                                                    ('Rozhodčí - člen klubu Plzeňský kraj - edit 3. třída', 'Rozhodci_ClenKlubuPlzenskyKraj_Edit3.Trida'),
                                                    ('Rozhodčí - člen klubu Praha - edit 3. třída', 'Rozhodci_ClenKlubuPraha_Edit3.Trida'),
                                                    ('Rozhodčí - člen klubu Středočeský kraj - edit 3. třída', 'Rozhodci_ClenKlubuStredoceskyKraj_Edit3.Trida'),
                                                    ('Rozhodčí - člen klubu Ústecký kraj - edit 3. třída', 'Rozhodci_ClenKlubuUsteckyKraj_Edit3.Trida'),
                                                    ('Rozhodčí - člen klubu Vysočina - edit 3. třída', 'Rozhodci_ClenKlubuVysocina_Edit3.Trida'),
                                                    ('Rozhodčí - člen klubu Zlínský kraj - edit 3. třída', 'Rozhodci_ClenKlubuZlinskyKraj_Edit3.Trida'),
                                                    ('Svazoví funkcionáři - vše - čtení', 'SvazoviFunkcionari_Vse_Cteni'),
                                                    ('Svazoví funkcionáři - vše - edit + přidání', 'SvazoviFunkcionari_Vse_Edit+Pridani'),
                                                    ('Trenéři - vše - čtení', 'Treneri_Vse_Cteni'),
                                                    ('Trenéři - vše - edit + přidání', 'Treneri_Vse_Edit+Pridani'),
                                                    ('Trenéři - vše - edit třídy a body', 'Treneri_Vse_EditTridyABody'),
                                                    ('Trenéři - člen klubu Jihočeský kraj - editace třídy + bodů - pouze 3. třída ', 'Treneri_ClenKlubuJihoceskyKraj_EditaceTridy+Bodu_Pouze3.Trida'),
                                                    ('Trenéři - člen klubu Jihomoravský kraj - editace třídy + bodů - pouze 3. třída ', 'Treneri_ClenKlubuJihomoravskyKraj_EditaceTridy+Bodu_Pouze3.Trida'),
                                                    ('Trenéři - člen klubu Karlovarský kraj - editace třídy + bodů - pouze 3. třída ', 'Treneri_ClenKlubuKarlovarskyKraj_EditaceTridy+Bodu_Pouze3.Trida'),
                                                    ('Trenéři - člen klubu Královéhradecký kraj - editace třídy + bodů - pouze 3. třída ', 'Treneri_ClenKlubuKralovehradeckyKraj_EditaceTridy+Bodu_Pouze3.Trida'),
                                                    ('Trenéři - člen klubu Liberecký kraj - editace třídy + bodů - pouze 3. třída ', 'Treneri_ClenKlubuLibereckyKraj_EditaceTridy+Bodu_Pouze3.Trida'),
                                                    ('Trenéři - člen klubu Moravskoslezský kraj - editace třídy + bodů - pouze 3. třída ', 'Treneri_ClenKlubuMoravskoslezskyKraj_EditaceTridy+Bodu_Pouze3.Trida'),
                                                    ('Trenéři - člen klubu Olomoucký kraj - editace třídy + bodů - pouze 3. třída ', 'Treneri_ClenKlubuOlomouckyKraj_EditaceTridy+Bodu_Pouze3.Trida'),
                                                    ('Trenéři - člen klubu Pardubický kraj - editace třídy + bodů - pouze 3. třída ', 'Treneri_ClenKlubuPardubickyKraj_EditaceTridy+Bodu_Pouze3.Trida'),
                                                    ('Trenéři - člen klubu Plzeňský kraj - editace třídy + bodů - pouze 3. třída ', 'Treneri_ClenKlubuPlzenskyKraj_EditaceTridy+Bodu_Pouze3.Trida'),
                                                    ('Trenéři - člen klubu Praha - editace třídy + bodů - pouze 3. třída ', 'Treneri_ClenKlubuPraha_EditaceTridy+Bodu_Pouze3.Trida'),
                                                    ('Trenéři - člen klubu Středočeský kraj - editace třídy + bodů - pouze 3. třída ', 'Treneri_ClenKlubuStredoceskyKraj_EditaceTridy+Bodu_Pouze3.Trida'),
                                                    ('Trenéři - člen klubu Ústecký kraj - editace třídy + bodů - pouze 3. třída ', 'Treneri_ClenKlubuUsteckyKraj_EditaceTridy+Bodu_Pouze3.Trida'),
                                                    ('Trenéři - člen klubu Vysočina - editace třídy + bodů - pouze 3. třída ', 'Treneri_ClenKlubuVysocina_EditaceTridy+Bodu_Pouze3.Trida'),
                                                    ('Trenéři - člen klubu Zlínský kraj - editace třídy + bodů - pouze 3. třída ', 'Treneri_ClenKlubuZlinskyKraj_EditaceTridy+Bodu_Pouze3.Trida'),
                                                    ('Zkušební komisaři - vše - čtení', 'ZkusebniKomisari_Vse_Cteni'),
                                                    ('Zkušební komisaři - vše - edit + přidání', 'ZkusebniKomisari_Vse_Edit+Pridani'),
                                                    ('Členové - vše - čtení + editace KYU', 'Clenove_Vse_Cteni+EditaceKyu'),
                                                    ('Zkušební komisaři - člen klubu Jihočeský kraj - editace tříd + přidání 3.třída', 'ZkusebniKomisari_ClenKlubuJihoceskyKraj_EditaceTrid+Pridani3.Trida'),
                                                    ('Zkušební komisaři - člen klubu Jihomoravský kraj - editace tříd + přidání 3.třída', 'ZkusebniKomisari_ClenKlubuJihomoravskyKraj_EditaceTrid+Pridani3.Trida'),
                                                    ('Zkušební komisaři - člen klubu Karlovarský kraj - editace tříd + přidání 3.třída', 'ZkusebniKomisari_ClenKlubuKarlovarskyKraj_EditaceTrid+Pridani3.Trida'),
                                                    ('Zkušební komisaři - člen klubu Královéhradecký kraj - editace tříd + přidání 3.třída', 'ZkusebniKomisari_ClenKlubuKralovehradeckyKraj_EditaceTrid+Pridani3.Trida'),
                                                    ('Zkušební komisaři - člen klubu Liberecký kraj - editace tříd + přidání 3.třída', 'ZkusebniKomisari_ClenKlubuLibereckyKraj_EditaceTrid+Pridani3.Trida'),
                                                    ('Zkušební komisaři - člen klubu Moravskoslezský kraj - editace tříd + přidání 3.třída', 'ZkusebniKomisari_ClenKlubuMoravskoslezskyKraj_EditaceTrid+Pridani3.Trida'),
                                                    ('Zkušební komisaři - člen klubu Olomoucký kraj - editace tříd + přidání 3.třída', 'ZkusebniKomisari_ClenKlubuOlomouckyKraj_EditaceTrid+Pridani3.Trida'),
                                                    ('Zkušební komisaři - člen klubu Pardubický kraj - editace tříd + přidání 3.třída', 'ZkusebniKomisari_ClenKlubuPardubickyKraj_EditaceTrid+Pridani3.Trida'),
                                                    ('Zkušební komisaři - člen klubu Plzeňský kraj - editace tříd + přidání 3.třída', 'ZkusebniKomisari_ClenKlubuPlzenskyKraj_EditaceTrid+Pridani3.Trida'),
                                                    ('Zkušební komisaři - člen klubu Praha - editace tříd + přidání 3.třída', 'ZkusebniKomisari_ClenKlubuPraha_EditaceTrid+Pridani3.Trida'),
                                                    ('Zkušební komisaři - člen klubu Středočeský kraj - editace tříd + přidání 3.třída', 'ZkusebniKomisari_ClenKlubuStredoceskyKraj_EditaceTrid+Pridani3.Trida'),
                                                    ('Zkušební komisaři - člen klubu Ústecký kraj - editace tříd + přidání 3.třída', 'ZkusebniKomisari_ClenKlubuUsteckyKraj_EditaceTrid+Pridani3.Trida'),
                                                    ('Zkušební komisaři - člen klubu Vysočina - editace tříd + přidání 3.třída', 'ZkusebniKomisari_ClenKlubuVysocina_EditaceTrid+Pridani3.Trida'),
                                                    ('Zkušební komisaři - člen klubu Zlínský kraj - editace tříd + přidání 3.třída', 'ZkusebniKomisari_ClenKlubuZlinskyKraj_EditaceTrid+Pridani3.Trida');

-------------------------------------------
----------------- MEMBERS -----------------

-- general access to 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 user_group WHERE name = 'Clenove_Vse_Cteni'), (SELECT id FROM agenda WHERE name = 'members'), TRUE, FALSE, FALSE, false),
       ((SELECT id FROM user_group WHERE name = 'Clenove_JihoceskyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), TRUE, true, FALSE, false),
       ((SELECT id FROM user_group WHERE name = 'Clenove_JihomoravskyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), TRUE, true, FALSE, false),
       ((SELECT id FROM user_group WHERE name = 'Clenove_KarlovarskyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), TRUE, true, FALSE, false),
       ((SELECT id FROM user_group WHERE name = 'Clenove_KralovehradeckyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), TRUE, true, FALSE, false),
       ((SELECT id FROM user_group WHERE name = 'Clenove_LibereckyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), TRUE, true, FALSE, false),
       ((SELECT id FROM user_group WHERE name = 'Clenove_MoravskoslezskyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), TRUE, true, FALSE, false),
       ((SELECT id FROM user_group WHERE name = 'Clenove_OlomouckyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), TRUE, true, FALSE, false),
       ((SELECT id FROM user_group WHERE name = 'Clenove_PardubickyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), TRUE, true, FALSE, false),
       ((SELECT id FROM user_group WHERE name = 'Clenove_PlzenskyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), TRUE, true, FALSE, false),
       ((SELECT id FROM user_group WHERE name = 'Clenove_Praha_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), TRUE, true, FALSE, false),
       ((SELECT id FROM user_group WHERE name = 'Clenove_StredoceskyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), TRUE, true, FALSE, false),
       ((SELECT id FROM user_group WHERE name = 'Clenove_UsteckyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), TRUE, true, FALSE, false),
       ((SELECT id FROM user_group WHERE name = 'Clenove_Vysocina_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), TRUE, true, FALSE, false),
       ((SELECT id FROM user_group WHERE name = 'Clenove_ZlinskyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), TRUE, true, FALSE, false),
       ((SELECT id FROM user_group WHERE name = 'Clenove_Vse_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'members'), TRUE, true, true, true),
       ((SELECT id FROM user_group WHERE name = 'Clenove_ClenoveKlubuAdmina_EditLimitovan+Pridani'), (SELECT id FROM agenda WHERE name = 'members'), TRUE, true, true, false),
       ((SELECT id FROM user_group WHERE name = 'Clenove_Vse_EditaceVyznamenaniKrajASvaz'), (SELECT id FROM agenda WHERE name = 'members'), TRUE, true, FALSE, false),
       ((SELECT id FROM user_group WHERE name = 'Clenove_Vse_PridaniDan'), (SELECT id FROM agenda WHERE name = 'members'), TRUE, true, FALSE, false),
       ((SELECT id FROM user_group WHERE name = 'Clenove_Vse_Cteni+EditaceKyu'), (SELECT id FROM agenda WHERE name = 'members'), TRUE, true, FALSE, false);

-- segment access to segments agenda
INSERT INTO user_group_has_agenda_has_segment_definition (id_user_group, id_agenda_has_segment_definition, is_read, is_write)
SELECT usr.id, ahsd.id, TRUE, FALSE
FROM (SELECT id
      FROM user_group
      WHERE name IN
            (
             'Clenove_JihoceskyKraj_Cteni+EditVyznamenaniKraj', 'Clenove_JihomoravskyKraj_Cteni+EditVyznamenaniKraj',
             'Clenove_KarlovarskyKraj_Cteni+EditVyznamenaniKraj', 'Clenove_KralovehradeckyKraj_Cteni+EditVyznamenaniKraj',
             'Clenove_LibereckyKraj_Cteni+EditVyznamenaniKraj', 'Clenove_MoravskoslezskyKraj_Cteni+EditVyznamenaniKraj',
             'Clenove_OlomouckyKraj_Cteni+EditVyznamenaniKraj', 'Clenove_PardubickyKraj_Cteni+EditVyznamenaniKraj',
             'Clenove_PlzenskyKraj_Cteni+EditVyznamenaniKraj', 'Clenove_Praha_Cteni+EditVyznamenaniKraj',
             'Clenove_StredoceskyKraj_Cteni+EditVyznamenaniKraj', 'Clenove_UsteckyKraj_Cteni+EditVyznamenaniKraj',
             'Clenove_Vysocina_Cteni+EditVyznamenaniKraj', 'Clenove_ZlinskyKraj_Cteni+EditVyznamenaniKraj'
                )) usr,
     agenda_has_segment_definition ahsd
         JOIN agenda_segment seg ON ahsd.id_agenda_segment = seg.id
WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'members')
  AND seg.name NOT IN ('honor_regional');

INSERT INTO user_group_has_agenda_has_segment_definition (id_user_group, id_agenda_has_segment_definition, is_read, is_write)
SELECT usr.id, ahsd.id, TRUE, FALSE
FROM (SELECT id
      FROM user_group
      WHERE name IN
            (
             'Clenove_ClenoveKlubuAdmina_EditLimitovan+Pridani'
                )) usr,
     agenda_has_segment_definition ahsd
         JOIN agenda_segment seg ON ahsd.id_agenda_segment = seg.id
WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'members')
  AND seg.name IN ('kyu', 'dan', 'honor_regional', 'honor_union');


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', 'suspended'),
    ('members_main', 'members_main', 'active_fighter'),
    ('members_main', 'members_main', 'honorary_member')
    );

INSERT INTO user_group_has_agenda_has_segment_definition (id_user_group, id_agenda_has_segment_definition, is_read, is_write)
SELECT usr.id, ahsd.id, TRUE, FALSE
FROM (SELECT id
      FROM user_group
      WHERE name IN
            (
                'Clenove_Vse_EditaceVyznamenaniKrajASvaz'
                )) usr,
     agenda_has_segment_definition ahsd
         JOIN agenda_segment seg ON ahsd.id_agenda_segment = seg.id
WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'members')
  AND seg.name NOT IN ('honor_regional', 'honor_union');

INSERT INTO user_group_has_agenda_has_segment_definition (id_user_group, id_agenda_has_segment_definition, is_read, is_write)
SELECT usr.id, ahsd.id, TRUE, FALSE
FROM (SELECT id
      FROM user_group
      WHERE name IN
            (
                'Clenove_Vse_PridaniDan'
                )) usr,
     agenda_has_segment_definition ahsd
         JOIN agenda_segment seg ON ahsd.id_agenda_segment = seg.id
WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'members')
  AND seg.name NOT IN ('dan');

INSERT INTO user_group_has_agenda_has_segment_definition (id_user_group, id_agenda_has_segment_definition, is_read, is_write)
SELECT usr.id, ahsd.id, TRUE, FALSE
FROM (SELECT id
      FROM user_group
      WHERE name IN
            (
                'Clenove_Vse_Cteni+EditaceKyu'
                )) usr,
     agenda_has_segment_definition ahsd
         JOIN agenda_segment seg ON ahsd.id_agenda_segment = seg.id
WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'members')
  AND seg.name NOT IN ('kyu');

-- entities limitations by definition value
INSERT INTO user_group_has_definition_value_for_aha (name, id_user_group, id_agenda, id_agenda_has_agenda, id_agenda_definition, value)
VALUES
    ('member_has_club_in_region_3_lvl_1', (SELECT id FROM user_group WHERE name = 'Clenove_JihoceskyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_3')),
    ('member_has_club_in_region_11_lvl_1', (SELECT id FROM user_group WHERE name = 'Clenove_JihomoravskyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_11')),
    ('member_has_club_in_region_5_lvl_1', (SELECT id FROM user_group WHERE name = 'Clenove_KarlovarskyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_5')),
    ('member_has_club_in_region_8_lvl_1', (SELECT id FROM user_group WHERE name = 'Clenove_KralovehradeckyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_8')),
    ('member_has_club_in_region_7_lvl_1', (SELECT id FROM user_group WHERE name = 'Clenove_LibereckyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_7')),
    ('member_has_club_in_region_14_lvl_1', (SELECT id FROM user_group WHERE name = 'Clenove_MoravskoslezskyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_14')),
    ('member_has_club_in_region_12_lvl_1', (SELECT id FROM user_group WHERE name = 'Clenove_OlomouckyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_12')),
    ('member_has_club_in_region_9_lvl_1', (SELECT id FROM user_group WHERE name = 'Clenove_PardubickyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_9')),
    ('member_has_club_in_region_4_lvl_1', (SELECT id FROM user_group WHERE name = 'Clenove_PlzenskyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_4')),
    ('member_has_club_in_region_1_lvl_1', (SELECT id FROM user_group WHERE name = 'Clenove_Praha_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_1')),
    ('member_has_club_in_region_2_lvl_1', (SELECT id FROM user_group WHERE name = 'Clenove_StredoceskyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_2')),
    ('member_has_club_in_region_6_lvl_1', (SELECT id FROM user_group WHERE name = 'Clenove_UsteckyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_6')),
    ('member_has_club_in_region_10_lvl_1', (SELECT id FROM user_group WHERE name = 'Clenove_Vysocina_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_10')),
    ('member_has_club_in_region_13_lvl_1', (SELECT id FROM user_group WHERE name = 'Clenove_ZlinskyKraj_Cteni+EditVyznamenaniKraj'), (SELECT id FROM agenda WHERE name = 'members'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_13'))
;

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
        ('member_has_club_and_admin_lvl1', null, (SELECT id FROM user_group WHERE name = 'Clenove_ClenoveKlubuAdmina_EditLimitovan+Pridani'), (SELECT id FROM agenda WHERE name = 'members'), (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
        ('member_has_club_and_admin_lvl2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = '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 = '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)
       ;

----------------- MEMBERS -----------------
-------------------------------------------

-------------------------------------------
------------ CLUB_ADMINISTRATOR -----------

-- general access to 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 user_group WHERE name = 'AdministratorKlubu_Vse_Cteni'), (SELECT id FROM agenda WHERE name = 'club_administrator'), TRUE, FALSE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'AdministratorKlubu_Vse_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'club_administrator'), TRUE, TRUE, TRUE, TRUE);

------------ CLUB_ADMINISTRATOR -----------
-------------------------------------------

-------------------------------------------
--------- TOURNAMENT_ADMINISTRATOR --------

-- general access to 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 user_group WHERE name = 'AdministratorTurnaje_Vse_Cteni'), (SELECT id FROM agenda WHERE name = 'tournament_administrator'), TRUE, FALSE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'AdministratorTurnaje_Vse_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'tournament_administrator'), TRUE, TRUE, TRUE, TRUE);

--------- TOURNAMENT_ADMINISTRATOR --------
-------------------------------------------

-------------------------------------------
--------------- UNION_FEES ----------------

-- general access to 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 user_group WHERE name = 'ClenskePoplatky_Vse_Cteni'), (SELECT id FROM agenda WHERE name = 'union_fees'), TRUE, FALSE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'ClenskePoplatky_Vse_EditaceFaktury'), (SELECT id FROM agenda WHERE name = 'union_fees'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'ClenskePoplatky_Vse_Zaplaceno+Zrusit'), (SELECT id FROM agenda WHERE name = 'union_fees'), TRUE, FALSE, FALSE, FALSE);

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
            (
                'ClenskePoplatky_Vse_EditaceFaktury'
                )) usr,
     mv_agenda_definition ad
WHERE ad.agenda_name = 'union_fees' AND (ad.segment_name, ad.attribute_group_name, ad.attribute_name) NOT IN (
        ('union_fees_main', 'union_fees_main', 'invoice')
    );

--------------- UNION_FEES ----------------
-------------------------------------------

-------------------------------------------
------------------ CLUBS ------------------

-- general access to 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 user_group WHERE name = 'Kluby_Vse_Cteni'), (SELECT id FROM agenda WHERE name = 'clubs'), TRUE, FALSE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'Kluby_Vse_Edit+Pridani+TiskKaret'), (SELECT id FROM agenda WHERE name = 'clubs'), TRUE, TRUE, TRUE, TRUE),
       ((SELECT id FROM user_group WHERE name = 'Kluby_AdminemKlubu_EditLimitovan+ObjednaniPoplatku+TiskKaret'), (SELECT id FROM agenda WHERE name = 'clubs'), TRUE, TRUE, true, 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)
SELECT usr.id, ahshagd.id, TRUE, FALSE
FROM (SELECT id
      FROM user_group
      WHERE name IN
            (
                'Kluby_AdminemKlubu_EditLimitovan+ObjednaniPoplatku+TiskKaret'
                )) 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 = 'clubs')
  AND (seg.name, grp.name) IN (('clubs_address', 'clubs_address_main'));

INSERT INTO user_group_has_agenda_has_segment_definition (id_user_group, id_agenda_has_segment_definition, is_read, is_write)
SELECT usr.id, ahsd.id, TRUE, FALSE
FROM (SELECT id
      FROM user_group
      WHERE name IN
            (
                'Kluby_AdminemKlubu_EditLimitovan+ObjednaniPoplatku+TiskKaret'
                )) usr,
     agenda_has_segment_definition ahsd
         JOIN agenda_segment seg ON ahsd.id_agenda_segment = seg.id
WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'clubs')
  AND seg.name IN ('club_statutory');


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
            (
                '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) NOT IN (
    ('clubs_main', 'email'),
    ('clubs_main', 'telephone'),
    ('avatar_group', 'avatar')
    );

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
    ('club_has_admin_lvl1', null, (SELECT id FROM user_group WHERE name = 'Kluby_AdminemKlubu_EditLimitovan+ObjednaniPoplatku+TiskKaret'), (SELECT id FROM agenda WHERE name = 'clubs'), (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)
;

------------------ CLUBS ------------------
-------------------------------------------

-------------------------------------------
----------------- REFEREE -----------------

-- general access to 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 user_group WHERE name = 'Rozhodci_Vse_Cteni'), (SELECT id FROM agenda WHERE name = 'referee'), TRUE, FALSE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'Rozhodci_Vse_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'referee'), TRUE, TRUE, TRUE, TRUE),
       ((SELECT id FROM user_group WHERE name = 'Rozhodci_Vse_EditLimitovan'), (SELECT id FROM agenda WHERE name = 'referee'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuJihoceskyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuJihomoravskyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuKarlovarskyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuKralovehradeckyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuLibereckyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuMoravskoslezskyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuOlomouckyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuPardubickyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuPlzenskyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuPraha_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuStredoceskyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuUsteckyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuVysocina_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuZlinskyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), TRUE, TRUE, FALSE, FALSE)
;


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
            (
                'Rozhodci_Vse_EditLimitovan'
                )) usr,
     mv_agenda_definition ad
WHERE ad.agenda_name = 'referee' AND (ad.segment_name, ad.attribute_group_name, ad.attribute_name) IN (
       ('referee_main', 'referee_main', 'members_has_referee'),
       ('referee_main', 'referee_main', 'appuser')
    );

INSERT INTO user_group_has_agenda_has_segment_definition (id_user_group, id_agenda_has_segment_definition, is_read, is_write)
SELECT usr.id, ahsd.id, TRUE, FALSE
FROM (SELECT id
      FROM user_group
      WHERE name IN
            (
             'Rozhodci_ClenKlubuJihoceskyKraj_Edit3.Trida',
             'Rozhodci_ClenKlubuJihomoravskyKraj_Edit3.Trida',
             'Rozhodci_ClenKlubuKarlovarskyKraj_Edit3.Trida',
             'Rozhodci_ClenKlubuKralovehradeckyKraj_Edit3.Trida',
             'Rozhodci_ClenKlubuLibereckyKraj_Edit3.Trida',
             'Rozhodci_ClenKlubuMoravskoslezskyKraj_Edit3.Trida',
             'Rozhodci_ClenKlubuOlomouckyKraj_Edit3.Trida',
             'Rozhodci_ClenKlubuPardubickyKraj_Edit3.Trida',
             'Rozhodci_ClenKlubuPlzenskyKraj_Edit3.Trida',
             'Rozhodci_ClenKlubuPraha_Edit3.Trida',
             'Rozhodci_ClenKlubuStredoceskyKraj_Edit3.Trida',
             'Rozhodci_ClenKlubuUsteckyKraj_Edit3.Trida',
             'Rozhodci_ClenKlubuVysocina_Edit3.Trida',
             'Rozhodci_ClenKlubuZlinskyKraj_Edit3.Trida'
                )) usr,
     agenda_has_segment_definition ahsd
         JOIN agenda_segment seg ON ahsd.id_agenda_segment = seg.id
WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'referee')
  AND seg.name NOT IN ('referee_class_main');

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
    ('referee_has_member_has_club_in_region_3_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuJihoceskyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_referee'), null, null),
    ('referee_has_member_has_club_in_region_11_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuJihomoravskyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_referee'), null, null),
    ('referee_has_member_has_club_in_region_5_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuKarlovarskyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_referee'), null, null),
    ('referee_has_member_has_club_in_region_8_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuKralovehradeckyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_referee'), null, null),
    ('referee_has_member_has_club_in_region_7_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuLibereckyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_referee'), null, null),
    ('referee_has_member_has_club_in_region_14_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuMoravskoslezskyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_referee'), null, null),
    ('referee_has_member_has_club_in_region_12_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuOlomouckyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_referee'), null, null),
    ('referee_has_member_has_club_in_region_9_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuPardubickyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_referee'), null, null),
    ('referee_has_member_has_club_in_region_4_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuPlzenskyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_referee'), null, null),
    ('referee_has_member_has_club_in_region_1_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuPraha_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_referee'), null, null),
    ('referee_has_member_has_club_in_region_2_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuStredoceskyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_referee'), null, null),
    ('referee_has_member_has_club_in_region_6_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuUsteckyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_referee'), null, null),
    ('referee_has_member_has_club_in_region_10_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuVysocina_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_referee'), null, null),
    ('referee_has_member_has_club_in_region_13_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuZlinskyKraj_Edit3.Trida'), (SELECT id FROM agenda WHERE name = 'referee'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_referee'), 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
    ('referee_has_member_has_club_in_region_3_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'referee_has_member_has_club_in_region_3_lvl_1'), (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuJihoceskyKraj_Edit3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_3')),
    ('referee_has_member_has_club_in_region_11_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'referee_has_member_has_club_in_region_11_lvl_1'), (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuJihomoravskyKraj_Edit3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_11')),
    ('referee_has_member_has_club_in_region_5_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'referee_has_member_has_club_in_region_5_lvl_1'), (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuKarlovarskyKraj_Edit3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_5')),
    ('referee_has_member_has_club_in_region_8_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'referee_has_member_has_club_in_region_8_lvl_1'), (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuKralovehradeckyKraj_Edit3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_8')),
    ('referee_has_member_has_club_in_region_7_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'referee_has_member_has_club_in_region_7_lvl_1'), (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuLibereckyKraj_Edit3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_7')),
    ('referee_has_member_has_club_in_region_14_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'referee_has_member_has_club_in_region_14_lvl_1'), (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuMoravskoslezskyKraj_Edit3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_14')),
    ('referee_has_member_has_club_in_region_12_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'referee_has_member_has_club_in_region_12_lvl_1'), (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuOlomouckyKraj_Edit3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_12')),
    ('referee_has_member_has_club_in_region_9_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'referee_has_member_has_club_in_region_9_lvl_1'), (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuPardubickyKraj_Edit3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_9')),
    ('referee_has_member_has_club_in_region_4_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'referee_has_member_has_club_in_region_4_lvl_1'), (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuPlzenskyKraj_Edit3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_4')),
    ('referee_has_member_has_club_in_region_1_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'referee_has_member_has_club_in_region_1_lvl_1'), (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuPraha_Edit3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_1')),
    ('referee_has_member_has_club_in_region_2_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'referee_has_member_has_club_in_region_2_lvl_1'), (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuStredoceskyKraj_Edit3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_2')),
    ('referee_has_member_has_club_in_region_6_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'referee_has_member_has_club_in_region_6_lvl_1'), (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuUsteckyKraj_Edit3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_6')),
    ('referee_has_member_has_club_in_region_10_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'referee_has_member_has_club_in_region_10_lvl_1'), (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuVysocina_Edit3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_10')),
    ('referee_has_member_has_club_in_region_13_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'referee_has_member_has_club_in_region_13_lvl_1'), (SELECT id FROM user_group WHERE name = 'Rozhodci_ClenKlubuZlinskyKraj_Edit3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_13'))
;

----------------- REFEREE -----------------
-------------------------------------------

-------------------------------------------
--------------- COMPETITIONS --------------

-- general access to 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 user_group WHERE name = 'SoutezeDlouhodobe_Vse_Cteni'), (SELECT id FROM agenda WHERE name = 'competitions'), TRUE, FALSE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'SoutezeDlouhodobe_Vse_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'competitions'), TRUE, TRUE, TRUE, TRUE);

--------------- COMPETITIONS --------------
-------------------------------------------

-------------------------------------------
----------------- OFFICIAL ----------------

-- general access to 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 user_group WHERE name = 'SvazoviFunkcionari_Vse_Cteni'), (SELECT id FROM agenda WHERE name = 'official'), TRUE, FALSE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'SvazoviFunkcionari_Vse_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'official'), TRUE, TRUE, TRUE, TRUE);

----------------- OFFICIAL ----------------
-------------------------------------------

-------------------------------------------
----------------- EXAMINER ----------------

-- general access to 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 user_group WHERE name = 'ZkusebniKomisari_Vse_Cteni'), (SELECT id FROM agenda WHERE name = 'examiner'), TRUE, FALSE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_Vse_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'examiner'), TRUE, TRUE, TRUE, TRUE),
       ((SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuJihoceskyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuJihomoravskyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuKarlovarskyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuKralovehradeckyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuLibereckyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuMoravskoslezskyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuOlomouckyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuPardubickyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuPlzenskyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuPraha_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuStredoceskyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuUsteckyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuVysocina_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), TRUE, TRUE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuZlinskyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), TRUE, TRUE, FALSE, FALSE)
;

INSERT INTO user_group_has_agenda_has_segment_definition (id_user_group, id_agenda_has_segment_definition, is_read, is_write)
SELECT usr.id, ahsd.id, TRUE, FALSE
FROM (SELECT id
      FROM user_group
      WHERE name IN
            (
             'ZkusebniKomisari_ClenKlubuJihoceskyKraj_EditaceTrid+Pridani3.Trida',
             'ZkusebniKomisari_ClenKlubuJihomoravskyKraj_EditaceTrid+Pridani3.Trida',
             'ZkusebniKomisari_ClenKlubuKarlovarskyKraj_EditaceTrid+Pridani3.Trida',
             'ZkusebniKomisari_ClenKlubuKralovehradeckyKraj_EditaceTrid+Pridani3.Trida',
             'ZkusebniKomisari_ClenKlubuLibereckyKraj_EditaceTrid+Pridani3.Trida',
             'ZkusebniKomisari_ClenKlubuMoravskoslezskyKraj_EditaceTrid+Pridani3.Trida',
             'ZkusebniKomisari_ClenKlubuOlomouckyKraj_EditaceTrid+Pridani3.Trida',
             'ZkusebniKomisari_ClenKlubuPardubickyKraj_EditaceTrid+Pridani3.Trida',
             'ZkusebniKomisari_ClenKlubuPlzenskyKraj_EditaceTrid+Pridani3.Trida',
             'ZkusebniKomisari_ClenKlubuPraha_EditaceTrid+Pridani3.Trida',
             'ZkusebniKomisari_ClenKlubuStredoceskyKraj_EditaceTrid+Pridani3.Trida',
             'ZkusebniKomisari_ClenKlubuUsteckyKraj_EditaceTrid+Pridani3.Trida',
             'ZkusebniKomisari_ClenKlubuVysocina_EditaceTrid+Pridani3.Trida',
             'ZkusebniKomisari_ClenKlubuZlinskyKraj_EditaceTrid+Pridani3.Trida'
                )) usr,
     agenda_has_segment_definition ahsd
         JOIN agenda_segment seg ON ahsd.id_agenda_segment = seg.id
WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'examiner')
  AND seg.name NOT IN ('examiner_class_main');

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
    ('examiner_has_member_has_club_in_region_3_lvl_1', null, (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuJihoceskyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_examiner'), null, null),
    ('examiner_has_member_has_club_in_region_11_lvl_1', null, (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuJihomoravskyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_examiner'), null, null),
    ('examiner_has_member_has_club_in_region_5_lvl_1', null, (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuKarlovarskyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_examiner'), null, null),
    ('examiner_has_member_has_club_in_region_8_lvl_1', null, (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuKralovehradeckyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_examiner'), null, null),
    ('examiner_has_member_has_club_in_region_7_lvl_1', null, (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuLibereckyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_examiner'), null, null),
    ('examiner_has_member_has_club_in_region_14_lvl_1', null, (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuMoravskoslezskyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_examiner'), null, null),
    ('examiner_has_member_has_club_in_region_12_lvl_1', null, (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuOlomouckyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_examiner'), null, null),
    ('examiner_has_member_has_club_in_region_9_lvl_1', null, (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuPardubickyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_examiner'), null, null),
    ('examiner_has_member_has_club_in_region_4_lvl_1', null, (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuPlzenskyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_examiner'), null, null),
    ('examiner_has_member_has_club_in_region_1_lvl_1', null, (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuPraha_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_examiner'), null, null),
    ('examiner_has_member_has_club_in_region_2_lvl_1', null, (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuStredoceskyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_examiner'), null, null),
    ('examiner_has_member_has_club_in_region_6_lvl_1', null, (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuUsteckyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_examiner'), null, null),
    ('examiner_has_member_has_club_in_region_10_lvl_1', null, (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuVysocina_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_examiner'), null, null),
    ('examiner_has_member_has_club_in_region_13_lvl_1', null, (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuZlinskyKraj_EditaceTrid+Pridani3.Trida'), (SELECT id FROM agenda WHERE name = 'examiner'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_examiner'), 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
    ('examiner_has_member_has_club_in_region_3_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'examiner_has_member_has_club_in_region_3_lvl_1'), (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuJihoceskyKraj_EditaceTrid+Pridani3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_3')),
    ('examiner_has_member_has_club_in_region_11_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'examiner_has_member_has_club_in_region_11_lvl_1'), (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuJihomoravskyKraj_EditaceTrid+Pridani3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_11')),
    ('examiner_has_member_has_club_in_region_5_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'examiner_has_member_has_club_in_region_5_lvl_1'), (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuKarlovarskyKraj_EditaceTrid+Pridani3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_5')),
    ('examiner_has_member_has_club_in_region_8_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'examiner_has_member_has_club_in_region_8_lvl_1'), (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuKralovehradeckyKraj_EditaceTrid+Pridani3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_8')),
    ('examiner_has_member_has_club_in_region_7_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'examiner_has_member_has_club_in_region_7_lvl_1'), (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuLibereckyKraj_EditaceTrid+Pridani3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_7')),
    ('examiner_has_member_has_club_in_region_14_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'examiner_has_member_has_club_in_region_14_lvl_1'), (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuMoravskoslezskyKraj_EditaceTrid+Pridani3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_14')),
    ('examiner_has_member_has_club_in_region_12_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'examiner_has_member_has_club_in_region_12_lvl_1'), (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuOlomouckyKraj_EditaceTrid+Pridani3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_12')),
    ('examiner_has_member_has_club_in_region_9_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'examiner_has_member_has_club_in_region_9_lvl_1'), (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuPardubickyKraj_EditaceTrid+Pridani3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_9')),
    ('examiner_has_member_has_club_in_region_4_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'examiner_has_member_has_club_in_region_4_lvl_1'), (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuPlzenskyKraj_EditaceTrid+Pridani3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_4')),
    ('examiner_has_member_has_club_in_region_1_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'examiner_has_member_has_club_in_region_1_lvl_1'), (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuPraha_EditaceTrid+Pridani3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_1')),
    ('examiner_has_member_has_club_in_region_2_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'examiner_has_member_has_club_in_region_2_lvl_1'), (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuStredoceskyKraj_EditaceTrid+Pridani3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_2')),
    ('examiner_has_member_has_club_in_region_6_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'examiner_has_member_has_club_in_region_6_lvl_1'), (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuUsteckyKraj_EditaceTrid+Pridani3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_6')),
    ('examiner_has_member_has_club_in_region_10_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'examiner_has_member_has_club_in_region_10_lvl_1'), (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuVysocina_EditaceTrid+Pridani3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_10')),
    ('examiner_has_member_has_club_in_region_13_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'examiner_has_member_has_club_in_region_13_lvl_1'), (SELECT id FROM user_group WHERE name = 'ZkusebniKomisari_ClenKlubuZlinskyKraj_EditaceTrid+Pridani3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_13'))
;

----------------- EXAMINER ----------------
-------------------------------------------

-------------------------------------------
----------------- PARENTS -----------------

-- general access to 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 user_group WHERE name = 'ZakonnyZastupce_Vse_Cteni'), (SELECT id FROM agenda WHERE name = 'parents'), TRUE, FALSE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'ZakonnyZastupce_Vse_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'parents'), TRUE, TRUE, TRUE, TRUE);

----------------- PARENTS -----------------
-------------------------------------------

-------------------------------------------
--------------- TOURNAMENTS ---------------

-- general access to 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 user_group WHERE name = 'Turnaje_Vse_Cteni'), (SELECT id FROM agenda WHERE name = 'tournaments'), TRUE, FALSE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_HlavnimRozhodcim_Cteni'), (SELECT id FROM agenda WHERE name = 'tournaments'), TRUE, false, false, false),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_Vse_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'tournaments'), TRUE, TRUE, TRUE, TRUE),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_TurnajAktivniAPoradanKlubemAdmina_Cteni+PrihlaseniClenuSvehoKlubu'), (SELECT id FROM agenda WHERE name = 'tournaments'), TRUE, false, false, false),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_AdminemTurnaje_Edit+Spusteni+TiskHesel'), (SELECT id FROM agenda WHERE name = 'tournaments'), TRUE, TRUE, false, false),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_JihoceskyKraj_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'tournaments'), TRUE, TRUE, TRUE, false),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_JihomoravskyKraj_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'tournaments'), TRUE, TRUE, TRUE, false),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_KarlovarskyKraj_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'tournaments'), TRUE, TRUE, TRUE, false),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_KralovehradeckyKraj_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'tournaments'), TRUE, TRUE, TRUE, false),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_LibereckyKraj_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'tournaments'), TRUE, TRUE, TRUE, false),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_MoravskoslezskyKraj_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'tournaments'), TRUE, TRUE, TRUE, false),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_OlomouckyKraj_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'tournaments'), TRUE, TRUE, TRUE, false),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_PardubickyKraj_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'tournaments'), TRUE, TRUE, TRUE, false),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_PlzenskyKraj_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'tournaments'), TRUE, TRUE, TRUE, false),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_Praha_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'tournaments'), TRUE, TRUE, TRUE, false),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_StredoceskyKraj_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'tournaments'), TRUE, TRUE, TRUE, false),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_UsteckyKraj_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'tournaments'), TRUE, TRUE, TRUE, false),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_Vysocina_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'tournaments'), TRUE, TRUE, TRUE, false),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_ZlinskyKraj_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'tournaments'), TRUE, TRUE, TRUE, false);

-- entities limitations by definition value
INSERT INTO user_group_has_definition_value (id_user_group, id_agenda_definition, value)
VALUES ((SELECT id FROM user_group WHERE name = 'Turnaje_JihoceskyKraj_Edit+Pridani'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournaments' AND segment_name = 'tournaments_main' AND attribute_group_name = 'tournaments_main' AND attribute_name = 'locations'), (SELECT id FROM lookup_locations WHERE name = 'region_3')),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_JihomoravskyKraj_Edit+Pridani'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournaments' AND segment_name = 'tournaments_main' AND attribute_group_name = 'tournaments_main' AND attribute_name = 'locations'), (SELECT id FROM lookup_locations WHERE name = 'region_11')),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_KarlovarskyKraj_Edit+Pridani'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournaments' AND segment_name = 'tournaments_main' AND attribute_group_name = 'tournaments_main' AND attribute_name = 'locations'), (SELECT id FROM lookup_locations WHERE name = 'region_5')),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_KralovehradeckyKraj_Edit+Pridani'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournaments' AND segment_name = 'tournaments_main' AND attribute_group_name = 'tournaments_main' AND attribute_name = 'locations'), (SELECT id FROM lookup_locations WHERE name = 'region_8')),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_LibereckyKraj_Edit+Pridani'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournaments' AND segment_name = 'tournaments_main' AND attribute_group_name = 'tournaments_main' AND attribute_name = 'locations'), (SELECT id FROM lookup_locations WHERE name = 'region_7')),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_MoravskoslezskyKraj_Edit+Pridani'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournaments' AND segment_name = 'tournaments_main' AND attribute_group_name = 'tournaments_main' AND attribute_name = 'locations'), (SELECT id FROM lookup_locations WHERE name = 'region_14')),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_OlomouckyKraj_Edit+Pridani'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournaments' AND segment_name = 'tournaments_main' AND attribute_group_name = 'tournaments_main' AND attribute_name = 'locations'), (SELECT id FROM lookup_locations WHERE name = 'region_12')),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_PardubickyKraj_Edit+Pridani'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournaments' AND segment_name = 'tournaments_main' AND attribute_group_name = 'tournaments_main' AND attribute_name = 'locations'), (SELECT id FROM lookup_locations WHERE name = 'region_9')),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_PlzenskyKraj_Edit+Pridani'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournaments' AND segment_name = 'tournaments_main' AND attribute_group_name = 'tournaments_main' AND attribute_name = 'locations'), (SELECT id FROM lookup_locations WHERE name = 'region_4')),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_Praha_Edit+Pridani'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournaments' AND segment_name = 'tournaments_main' AND attribute_group_name = 'tournaments_main' AND attribute_name = 'locations'), (SELECT id FROM lookup_locations WHERE name = 'region_1')),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_StredoceskyKraj_Edit+Pridani'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournaments' AND segment_name = 'tournaments_main' AND attribute_group_name = 'tournaments_main' AND attribute_name = 'locations'), (SELECT id FROM lookup_locations WHERE name = 'region_2')),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_UsteckyKraj_Edit+Pridani'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournaments' AND segment_name = 'tournaments_main' AND attribute_group_name = 'tournaments_main' AND attribute_name = 'locations'), (SELECT id FROM lookup_locations WHERE name = 'region_6')),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_Vysocina_Edit+Pridani'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournaments' AND segment_name = 'tournaments_main' AND attribute_group_name = 'tournaments_main' AND attribute_name = 'locations'), (SELECT id FROM lookup_locations WHERE name = 'region_10')),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_ZlinskyKraj_Edit+Pridani'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournaments' AND segment_name = 'tournaments_main' AND attribute_group_name = 'tournaments_main' AND attribute_name = 'locations'), (SELECT id FROM lookup_locations WHERE name = 'region_13'))
;

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
    ('tournament_has_referee_lvl1', null, (SELECT id FROM user_group WHERE name = 'Turnaje_HlavnimRozhodcim_Cteni'), (SELECT id FROM agenda WHERE name = 'tournaments'), (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_referee'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'referee' AND segment_name = 'referee_main' AND attribute_group_name = 'referee_main' AND attribute_name = 'appuser') , 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
    ('tournament_has_admin_lvl1', null, (SELECT id FROM user_group WHERE name = 'Turnaje_AdminemTurnaje_Edit+Spusteni+TiskHesel'), (SELECT id FROM agenda WHERE name = 'tournaments'), (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_tournament_administrator'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournament_administrator' AND segment_name = 'tournament_administrator_main' AND attribute_group_name = 'tournament_administrator_main' AND attribute_name = 'appuser') , 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
    ('tournament_has_club_has_admin_lvl1', null, (SELECT id FROM user_group WHERE name = 'Turnaje_TurnajAktivniAPoradanKlubemAdmina_Cteni+PrihlaseniClenuSvehoKlubu'), (SELECT id FROM agenda WHERE name = 'tournaments'), (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_clubs'), 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
    ('tournament_has_club_has_admin_lvl2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'tournament_has_club_has_admin_lvl1'), (SELECT id FROM user_group WHERE name = 'Turnaje_TurnajAktivniAPoradanKlubemAdmina_Cteni+PrihlaseniClenuSvehoKlubu'), 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)
;

INSERT INTO user_group_has_definition_value (id_user_group, id_agenda_definition, value)
VALUES ((SELECT id FROM user_group WHERE name = 'Turnaje_TurnajAktivniAPoradanKlubemAdmina_Cteni+PrihlaseniClenuSvehoKlubu'), (SELECT id FROM mv_agenda_definition WHERE agenda_name = 'tournaments' AND segment_name = 'tournament_information' AND attribute_group_name = 'tournament_information_main' AND attribute_name = 'is_active'), '1');


--------------- TOURNAMENTS ---------------
-------------------------------------------

-------------------------------------------
----------------- TRAINER -----------------

-- general access to 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 user_group WHERE name = 'Treneri_Vse_Cteni'), (SELECT id FROM agenda WHERE name = 'trainer'), TRUE, FALSE, FALSE, FALSE),
       ((SELECT id FROM user_group WHERE name = 'Treneri_Vse_Edit+Pridani'), (SELECT id FROM agenda WHERE name = 'trainer'), TRUE, TRUE, TRUE, TRUE),
       ((SELECT id FROM user_group WHERE name = 'Treneri_Vse_EditTridyABody'), (SELECT id FROM agenda WHERE name = 'trainer'), TRUE, TRUE, false, false),
       ((SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuJihoceskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), TRUE, TRUE, false, false),
       ((SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuJihomoravskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), TRUE, TRUE, false, false),
       ((SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuKarlovarskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), TRUE, TRUE, false, false),
       ((SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuKralovehradeckyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), TRUE, TRUE, false, false),
       ((SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuLibereckyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), TRUE, TRUE, TRUE, false),
       ((SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuMoravskoslezskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), TRUE, TRUE, false, false),
       ((SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuOlomouckyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), TRUE, TRUE, false, false),
       ((SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuPardubickyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), TRUE, TRUE, false, false),
       ((SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuPlzenskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), TRUE, TRUE, false, false),
       ((SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuPraha_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), TRUE, TRUE, false, false),
       ((SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuStredoceskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), TRUE, TRUE, false, false),
       ((SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuUsteckyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), TRUE, TRUE, false, false),
       ((SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuVysocina_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), TRUE, TRUE, false, false),
       ((SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuZlinskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), TRUE, TRUE, false, false);


INSERT INTO user_group_has_agenda_has_segment_definition (id_user_group, id_agenda_has_segment_definition, is_read, is_write)
SELECT usr.id, ahsd.id, TRUE, FALSE
FROM (SELECT id
      FROM user_group
      WHERE name IN
            (
             'Treneri_Vse_EditTridyABody',
             'Treneri_ClenKlubuJihoceskyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuJihomoravskyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuKarlovarskyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuKralovehradeckyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuLibereckyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuMoravskoslezskyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuOlomouckyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuPardubickyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuPlzenskyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuPraha_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuStredoceskyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuUsteckyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuVysocina_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuZlinskyKraj_EditaceTridy+Bodu_Pouze3.Trida'
                )) usr,
     agenda_has_segment_definition ahsd
         JOIN agenda_segment seg ON ahsd.id_agenda_segment = seg.id
WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'trainer')
  AND seg.name NOT IN ('trainer_class_main', 'credit_point_main');



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
    ('trainer_has_member_has_club_in_region_3_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuJihoceskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_trainer'), null, null),
    ('trainer_has_member_has_club_in_region_11_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuJihomoravskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_trainer'), null, null),
    ('trainer_has_member_has_club_in_region_5_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuKarlovarskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_trainer'), null, null),
    ('trainer_has_member_has_club_in_region_8_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuKralovehradeckyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_trainer'), null, null),
    ('trainer_has_member_has_club_in_region_7_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuLibereckyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_trainer'), null, null),
    ('trainer_has_member_has_club_in_region_14_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuMoravskoslezskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_trainer'), null, null),
    ('trainer_has_member_has_club_in_region_12_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuOlomouckyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_trainer'), null, null),
    ('trainer_has_member_has_club_in_region_9_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuPardubickyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_trainer'), null, null),
    ('trainer_has_member_has_club_in_region_4_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuPlzenskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_trainer'), null, null),
    ('trainer_has_member_has_club_in_region_1_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuPraha_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_trainer'), null, null),
    ('trainer_has_member_has_club_in_region_2_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuStredoceskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_trainer'), null, null),
    ('trainer_has_member_has_club_in_region_6_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuUsteckyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_trainer'), null, null),
    ('trainer_has_member_has_club_in_region_10_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuVysocina_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_trainer'), null, null),
    ('trainer_has_member_has_club_in_region_13_lvl_1', null, (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuZlinskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), (SELECT id FROM agenda WHERE name = 'trainer'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_trainer'), 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
    ('trainer_has_member_has_club_in_region_3_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'trainer_has_member_has_club_in_region_3_lvl_1'), (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuJihoceskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_3')),
    ('trainer_has_member_has_club_in_region_11_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'trainer_has_member_has_club_in_region_11_lvl_1'), (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuJihomoravskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_11')),
    ('trainer_has_member_has_club_in_region_5_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'trainer_has_member_has_club_in_region_5_lvl_1'), (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuKarlovarskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_5')),
    ('trainer_has_member_has_club_in_region_8_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'trainer_has_member_has_club_in_region_8_lvl_1'), (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuKralovehradeckyKraj_EditaceTridy+Bodu_Pouze3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_8')),
    ('trainer_has_member_has_club_in_region_7_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'trainer_has_member_has_club_in_region_7_lvl_1'), (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuLibereckyKraj_EditaceTridy+Bodu_Pouze3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_7')),
    ('trainer_has_member_has_club_in_region_14_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'trainer_has_member_has_club_in_region_14_lvl_1'), (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuMoravskoslezskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_14')),
    ('trainer_has_member_has_club_in_region_12_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'trainer_has_member_has_club_in_region_12_lvl_1'), (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuOlomouckyKraj_EditaceTridy+Bodu_Pouze3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_12')),
    ('trainer_has_member_has_club_in_region_9_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'trainer_has_member_has_club_in_region_9_lvl_1'), (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuPardubickyKraj_EditaceTridy+Bodu_Pouze3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_9')),
    ('trainer_has_member_has_club_in_region_4_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'trainer_has_member_has_club_in_region_4_lvl_1'), (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuPlzenskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_4')),
    ('trainer_has_member_has_club_in_region_1_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'trainer_has_member_has_club_in_region_1_lvl_1'), (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuPraha_EditaceTridy+Bodu_Pouze3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_1')),
    ('trainer_has_member_has_club_in_region_2_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'trainer_has_member_has_club_in_region_2_lvl_1'), (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuStredoceskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_2')),
    ('trainer_has_member_has_club_in_region_6_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'trainer_has_member_has_club_in_region_6_lvl_1'), (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuUsteckyKraj_EditaceTridy+Bodu_Pouze3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_6')),
    ('trainer_has_member_has_club_in_region_10_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'trainer_has_member_has_club_in_region_10_lvl_1'), (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuVysocina_EditaceTridy+Bodu_Pouze3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_10')),
    ('trainer_has_member_has_club_in_region_13_lvl_2', (SELECT id FROM user_group_has_definition_value_for_aha WHERE name = 'trainer_has_member_has_club_in_region_13_lvl_1'), (SELECT id FROM user_group WHERE name = 'Treneri_ClenKlubuZlinskyKraj_EditaceTridy+Bodu_Pouze3.Trida'), null, (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), (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') , (SELECT id FROM lookup_locations WHERE name = 'region_13'))
;

----------------- TRAINER -----------------
-------------------------------------------


-- view refresh
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;

-- #13634 - permissions --
--------------------------


----------------------------------------------------
-- 2.6.4.0
-- SQL for this version ends here
----------------------------------------------------


-- #13599 --
INSERT INTO agenda_button (id_agenda, name, description, icon, class) VALUES ((SELECT id FROM agenda WHERE name = 'members'), 'MemberCard', 'Tisk Členské karty', 'fal fa-id-card', 'btn-info');
INSERT INTO agenda_action (name, description, class_name, is_via_rabbit) VALUES ('MemberCard', 'Tisk Členské karty', 'MemberCard', FALSE);
INSERT INTO agenda_button_has_action (id_agenda_button, id_agenda_action, priority) VALUES ((SELECT id FROM agenda_button WHERE name = 'MemberCard'), (SELECT id FROM agenda_action WHERE name = 'MemberCard'), 10);

INSERT INTO agenda_has_agenda_custom_row_button (id_agenda_has_agenda, class_name) VALUES ((SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_union_fees'), 'MembersCardsBulk');

----------------------------------------------------
-- 2.6.5.0
-- SQL for this version ends here
----------------------------------------------------

INSERT INTO flexii.agenda_button (id_agenda, name, description, icon, class) VALUES ((SELECT id FROM agenda WHERE name = 'clubs'), 'ClubMemberFees', 'Poplatky', 'fal fa-id-card', 'btn-info');
INSERT INTO flexii.agenda_action (name, description, class_name, is_via_rabbit) VALUES ('ClubMemberFees', 'Poplatky', 'ClubMemberFees', true);
INSERT INTO flexii.agenda_button_has_action (id_agenda_button, id_agenda_action, priority) VALUES ((SELECT id FROM agenda_button WHERE name = 'ClubMemberFees'), (SELECT id FROM agenda_action WHERE name = 'ClubMemberFees'), 10);


UPDATE configuration.configuration_definition
SET is_not_null = true
WHERE id_configuration_group = (SELECT id FROM configuration.configuration_group WHERE name = 'bookkeeping');

-- #14138
insert into attribute (name, description, data_type_precision, lookup, lookup_columns, weight, id_attribute_data_type)
select
    'club_gps_x'
     , 'GPS Souřadnice X'
     , 0
     , null
     , null
     , 1
     , (select id from attribute_data_type where name = 'string')
where not exists (
    select id from attribute where name = 'club_gps_x'
);

insert into attribute (name, description, data_type_precision, lookup, lookup_columns, weight, id_attribute_data_type)
select
    'club_gps_y'
     , 'GPS Souřadnice y'
     , 0
     , null
     , null
     , 1
     , (select id from attribute_data_type where name = 'string')
where not exists (
    select id from attribute where name = 'club_gps_y'
);

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_hidden_in_datagrid, is_search_by
  , in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition
) select
        (select id from attribute where name = 'club_gps_x')
      , (select max(priority) + 10 from mv_agenda_definition where agenda_name = 'clubs' and segment_name = 'clubs_main' and attribute_group_name = 'clubs_main')
      , false, null, false, true, null, 3, 0, 1, true, true, false, null, false, true
      , (select id from agenda_has_segment_has_attribute_group_definition where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'clubs') and id_agenda_segment = (select id from agenda_segment where name = 'clubs_main')) and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'clubs_main'))
where not exists (
    select id from agenda_definition where id_attribute = (select id from attribute where name = 'club_gps_x')
);

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_hidden_in_datagrid, is_search_by
  , in_title_and_priority, is_unique, is_importable, id_agenda_segment_attribute_group_definition
) select
        (select id from attribute where name = 'club_gps_y')
      , (select max(priority) + 10 from mv_agenda_definition where agenda_name = 'clubs' and segment_name = 'clubs_main' and attribute_group_name = 'clubs_main')
      , false, null, false, true, null, 3, 0, 1, true, true, false, null, false, true
      , (select id from agenda_has_segment_has_attribute_group_definition where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'clubs') and id_agenda_segment = (select id from agenda_segment where name = 'clubs_main')) and id_agenda_attribute_group = (select id from agenda_attribute_group where name = 'clubs_main'))
where not exists (
    select id from agenda_definition where id_attribute = (select id from attribute where name = 'club_gps_y')
);

update agenda_definition set is_in_datagrid = true, is_hidden_in_datagrid = true
where id in (
SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'clubs'
   AND segment_name = 'clubs_main'
   AND attribute_group_name = 'clubs_main'
   AND attribute_name in ( 'telephone', 'telephone_2', 'email', 'account_number')
);

update agenda_definition set is_in_datagrid = true, is_hidden_in_datagrid = true
where id in (
SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'clubs'
   AND segment_name = 'clubs_address'
   AND attribute_group_name = 'clubs_address_main'
   AND attribute_name in ('street', 'street_number', 'house_number', 'city_area', 'city', 'postal_code', 'address_region', 'country')
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

----------------------------------------------------
-- 2.6.5.2
-- SQL for this version ends here
----------------------------------------------------

update agenda_definition set is_not_null = FALSE
where id in (
    SELECT id
    FROM mv_agenda_definition
    WHERE agenda_name = 'members'
      AND segment_name = 'members_main'
      AND attribute_group_name = 'members_address_main'
      AND attribute_name = 'address_region'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

UPDATE flexii.user_group_has_agenda
SET is_can_create = FALSE
WHERE id_user_group = (SELECT id FROM user_group WHERE name = 'Kluby_AdminemKlubu_EditLimitovan+ObjednaniPoplatku+TiskKaret')
  AND id_agenda = (SELECT id FROM agenda WHERE name = 'clubs');

UPDATE flexii.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 = 'members');

----------------------------------------------------
-- 2.6.5.4
-- SQL for this version ends here
----------------------------------------------------

INSERT INTO flexii.master_lookup (lookup_name, name, description, priority) VALUES ('lookup_states_of_payment', 'processing', 'Zpracovává se', 5);

UPDATE agenda_definition SET default_value = (SELECT id FROM master_lookup WHERE lookup_name = 'lookup_states_of_payment' AND name = 'processing')
                         WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'union_fees-union_fees_main-union_fees_main-state_of_payment');

REFRESH MATERIALIZED VIEW mv_agenda_definition;

--- 14162 ---
UPDATE agenda_definition
    SET file_specification = 'doc,docx,xls,xlsx,ppt,pptx,pdf,txt,odt,odp,ods,csv,xml,html,htm,eps,rtf,log,md,json,jpg,jpeg,png,gif,heic'
WHERE 1=1
AND id_attribute = (select id from attribute where name = 'attached_files')
AND id_agenda_segment_attribute_group_definition = (
    select id from agenda_has_segment_has_attribute_group_definition
    where id_agenda_has_segment_definition = (
        select id from agenda_has_segment_definition
            where id_agenda = (select id from agenda where name = 'clubs')
            and id_agenda_segment = (select id from agenda_segment where name = 'club_document')
    )
    and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'club_document')
);

UPDATE agenda_definition
    SET file_specification = 'doc,docx,xls,xlsx,ppt,pptx,pdf,txt,odt,odp,ods,csv,xml,html,htm,eps,rtf,log,md,json,jpg,jpeg,png,gif,heic'
WHERE 1=1
AND id_attribute = (select id from attribute where name = 'statutes')
AND id_agenda_segment_attribute_group_definition = (
    select id from agenda_has_segment_has_attribute_group_definition
    where id_agenda_has_segment_definition = (
        select id from agenda_has_segment_definition
            where id_agenda = (select id from agenda where name = 'clubs')
            and id_agenda_segment = (select id from agenda_segment where name = 'club_statutes')
    )
    and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'club_statutes_main')
);

UPDATE agenda_definition
    SET file_specification = 'doc,docx,xls,xlsx,ppt,pptx,pdf,txt,odt,odp,ods,csv,xml,html,htm,eps,rtf,log,md,json,jpg,jpeg,png,gif,heic'
WHERE 1=1
AND id_attribute = (select id from attribute where name = 'proposition')
AND id_agenda_segment_attribute_group_definition = (
    select id from agenda_has_segment_has_attribute_group_definition
    where id_agenda_has_segment_definition = (
        select id from agenda_has_segment_definition
            where id_agenda = (select id from agenda where name = 'tournaments')
            and id_agenda_segment = (select id from agenda_segment where name = 'tournament_proposition')
    )
    and id_agenda_attribute_group	= (select id from agenda_attribute_group where name = 'tournament_proposition_main')
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

----------------------------------------------------
-- 2.6.5.5
-- SQL for this version ends here
----------------------------------------------------

UPDATE configuration.configuration_definition SET is_not_null = false WHERE id_configuration_group = (SELECT id FROM configuration.configuration_group WHERE name = 'csju_mssql_db_snapshot_server');
UPDATE configuration.configuration_definition SET default_value = null, example = '25' WHERE name IN ('mail_port', 'mail_log_port');

update agenda_definition_specification_by_value
set value_control = 1
where id_agenda_definition = (SELECT id
                              FROM mv_agenda_definition
                              WHERE agenda_name = 'members'
                                AND segment_name = 'members_main'
                                AND attribute_group_name = 'members_main'
                                AND attribute_name = 'members_has_parents')
  AND id_agenda_definition_control = (SELECT id
                                      FROM mv_agenda_definition
                                      WHERE agenda_name = 'members'
                                        AND segment_name = 'members_main'
                                        AND attribute_group_name = 'members_main'
                                        AND attribute_name = 'nonage')
  AND is_not_null = true;


update agenda_button
set confirmation = 'Tato akce je nevratná. Jste si jistí?'
where name = 'TournamentCancel';

----------------------------------------------------
-- 2.6.5.8
-- SQL for this version ends here
----------------------------------------------------

update agenda_definition set enabled = false
where id = (
SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'trainer'
   AND segment_name = 'trainer_main'
   AND attribute_group_name = 'trainer_main'
   AND attribute_name = 'id_trainer'
);

update agenda_definition set enabled = false
where id = (
SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'referee'
   AND segment_name = 'referee_main'
   AND attribute_group_name = 'referee_main'
   AND attribute_name = 'id_referee'
);

update agenda_definition set enabled = false
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 = 'id_examiner'
);

REFRESH MATERIALIZED VIEW mv_agenda_definition;

-- #14117
MERGE INTO custom_import_row d
USING (SELECT cir.id, ae.uid
       FROM custom_import_row cir
                JOIN agenda_entity ae ON ae.id = cir.id_agenda_entity
       WHERE agenda_entity_uid IS NULL) s
ON s.id = d.id
WHEN MATCHED THEN
    UPDATE SET agenda_entity_uid = s.uid;

----------------------------------------------------
-- 2.6.6.0
-- SQL for this version ends here
----------------------------------------------------

-- #14173 wu18 update
UPDATE lookup_weightcategories set weight_from = 45
WHERE id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu18')
AND name = '-48 kg';

UPDATE lookup_weightcategories set id_lookup_weightcategories_plus = null
WHERE id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu18')
AND name = '-70 kg';

UPDATE lookup_weightcategories set is_hidden = false
WHERE id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu18')
AND name = '+70 kg';

INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu18'), '+40 kg', '+40 kg', 41, 999, true, 3);
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu18'), '+44 kg', '+44 kg', 45, 999, true, 7);

INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu18'), '-40 kg', '-40 kg', 0, 40, false, 2, (SELECT id FROM lookup_weightcategories WHERE name = '+40 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu18')));
INSERT INTO lookup_weightcategories (id_lookup_categories, name, description, weight_from, weight_to, is_hidden, priority, id_lookup_weightcategories_plus) VALUES((SELECT id FROM lookup_categories WHERE name = '2023_wu18'), '-44 kg', '-44 kg', 41, 44, false, 5, (SELECT id FROM lookup_weightcategories WHERE name = '+44 kg' AND id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu18')));

REFRESH MATERIALIZED VIEW lookup_judo_categories;

-- migrate old values --
-- PROD version --
update agenda_has_agenda_values set value = (select id::varchar from lookup_judo_categories where name in ('2023_wu18_+70 kg')) where id in (
     select id from agenda_has_agenda_values where id_agenda_entity_has_agenda_entity in (
        select id from agenda_entity_has_agenda_entity
          where id_agenda_entity_linked in (
              select distinct(id_agenda_entity) from agenda_values where id_agenda_definition = (
                select id from mv_agenda_definition where id_attribute in (
                    select id from attribute where lookup = 'lookup_judo_categories'
                )
              ) and value in (select id::varchar from lookup_judo_categories where name in ('2023_wu18_-78 kg', '2023_wu18_+78 kg'))
          )
          and id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'tournaments_has_members')
    ) and id_agenda_has_agenda_definition = (
        select id from agenda_has_agenda_definition
             where id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'tournaments_has_members')
                and id_attribute = (select id from attribute where name = 'weight_category')
    ) and value in (select id::varchar from lookup_judo_categories where name in ('2023_wu18_-78 kg', '2023_wu18_+78 kg'))
);

-- delete agenda_values removed category + date from/to
delete from agenda_values where id in (
    select id from agenda_values where (id_agenda_entity, recurring_sequence) in (
         select id_agenda_entity, recurring_sequence from agenda_values where id_agenda_definition = (
            select id from mv_agenda_definition where id_attribute in (
                select id from attribute where lookup = 'lookup_judo_categories'
            )
        ) and value in (select id::varchar from lookup_judo_categories where name in ('2023_wu18_-78 kg'))
    )
);

-- change value in recurring group to max weight in category
update agenda_values set value = (select id::varchar from lookup_judo_categories where name in ('2023_wu18_+70 kg')) where id in (
    select id from agenda_values where id_agenda_definition = (
        select id from mv_agenda_definition where id_attribute in (
            select id from attribute where lookup = 'lookup_judo_categories'
        )
    ) and value in (select id::varchar from lookup_judo_categories where name in ('2023_wu18_+78 kg'))
);

-- compare select --
-- select * from agenda_values where id_agenda_definition = (
--     select id from mv_agenda_definition where id_attribute in (
--         select id from attribute where lookup = 'lookup_judo_categories'
--     )
-- ) and value in (select id::varchar from lookup_judo_categories where name in ('2023_wu18_-78 kg', '2023_wu18_+78 kg'));

-- DELETE FROM agenda_values WHERE id_agenda_definition = (
--     select id from mv_agenda_definition where id_attribute in (
--         select id from attribute where lookup = 'lookup_judo_categories'
--     )
-- ) and value in (select id::varchar from lookup_judo_categories where name in ('2023_wu18_-78 kg', '2023_wu18_+78 kg'));

-- DELETE FROM lookup_weightcategories WHERE id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu18') AND name = '-78 kg';
-- DELETE FROM lookup_weightcategories WHERE id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu18') AND name = '+78 kg';

update lookup_weightcategories set enabled = false WHERE id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu18') AND name = '-78 kg';
update lookup_weightcategories set enabled = false WHERE id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu18') AND name = '+78 kg';

REFRESH MATERIALIZED VIEW lookup_judo_categories;

-- #14244
UPDATE agenda_has_agenda SET relation = 4, is_unique = true WHERE name = 'club_has_administrator';

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 = 'Clenove_Vse_Edit+Pridani'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), TRUE, true, true, true),
       ((SELECT id FROM user_group WHERE name = 'Clenove_ClenoveKlubuAdmina_EditLimitovan+Pridani'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members'), TRUE, true, false, false),
       ((SELECT id FROM user_group WHERE name = 'Clenove_Vse_Edit+Pridani'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_officials_has_members'), TRUE, TRUE, TRUE, TRUE),
       ((SELECT id FROM user_group WHERE name = 'Clenove_ClenoveKlubuAdmina_EditLimitovan+Pridani'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_officials_has_members'), TRUE, TRUE, TRUE, TRUE),
       ((SELECT id FROM user_group WHERE name = 'Clenove_ClenoveKlubuAdmina_EditLimitovan+Pridani'), (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members_hosting'), TRUE, true, TRUE, false),
       ((SELECT id FROM user_group WHERE name = 'Clenove_ClenoveKlubuAdmina_EditLimitovan+Pridani'), (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_members'), TRUE, true, TRUE, TRUE),
       ((SELECT id FROM user_group WHERE name = 'Turnaje_TurnajAktivniAPoradanKlubemAdmina_Cteni+PrihlaseniClenuSvehoKlubu'), (SELECT id FROM agenda_has_agenda WHERE name = 'tournaments_has_members'), TRUE, TRUE, TRUE, TRUE);


MERGE INTO user_group_has_agenda_has_agenda d
USING
    (SELECT id_user_group, id_agenda_has_agenda, is_can_read, is_can_write, is_can_create, is_can_delete
     FROM user_group_has_agenda_has_agenda
     WHERE ENABLED = TRUE
     UNION
     SELECT grp.id AS id_user_group
          , a.id   AS id_agenda_has_agenda
          , TRUE   AS is_can_read
          , FALSE  AS is_can_write
          , FALSE  AS is_can_create
          , FALSE  AS is_can_delete
     FROM user_group grp,
          agenda_has_agenda a
     WHERE (grp.id, a.id) NOT IN (SELECT id_user_group, id_agenda_has_agenda FROM user_group_has_agenda_has_agenda WHERE ENABLED = TRUE)) s
ON s.id_user_group = d.id_user_group AND s.id_agenda_has_agenda = d.id_agenda_has_agenda
WHEN NOT MATCHED THEN
    INSERT ( id_user_group
           , id_agenda_has_agenda
           , is_can_read
           , is_can_write
           , is_can_create
           , is_can_delete
    )
    VALUES ( s.id_user_group
           , s.id_agenda_has_agenda
           , s.is_can_read
           , s.is_can_write
           , s.is_can_create
           , s.is_can_delete
           );

REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_has_agenda;

----------------------------------------------------
-- 2.6.6.1
-- SQL for this version ends here
----------------------------------------------------
----------------------------------------------------
-- 2.6.6.3
-- SQL for this version ends here
----------------------------------------------------

-- #14173 - new development
-- mu16 remove -38 kg & +38 kg

UPDATE lookup_weightcategories set weight_from = 0
WHERE id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu16')
AND name = '-42 kg';

-- migrate old values --
-- PROD version --
update agenda_has_agenda_values set value = (select id::varchar from lookup_judo_categories where name in ('2023_mu16_-42 kg')) where id in (
     select id from agenda_has_agenda_values where id_agenda_entity_has_agenda_entity in (
        select id from agenda_entity_has_agenda_entity
          where id_agenda_entity_linked in (
              select distinct(id_agenda_entity) from agenda_values where id_agenda_definition = (
                select id from mv_agenda_definition where id_attribute in (
                    select id from attribute where lookup = 'lookup_judo_categories'
                )
              ) and value in (select id::varchar from lookup_judo_categories where name in ('2023_mu16_-38 kg', '2023_mu16_+38 kg', '2023_mu16_-40 kg', '2023_mu16_+40 kg'))
          )
          and id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'tournaments_has_members')
    ) and id_agenda_has_agenda_definition = (
        select id from agenda_has_agenda_definition
             where id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'tournaments_has_members')
                and id_attribute = (select id from attribute where name = 'weight_category')
    ) and value in (select id::varchar from lookup_judo_categories where name in ('2023_mu16_-38 kg', '2023_mu16_+38 kg'))
);

-- delete agenda_values removed category + date from/to
delete from agenda_values where id in (
    select id from agenda_values where (id_agenda_entity, recurring_sequence) in (
         select id_agenda_entity, recurring_sequence from agenda_values where id_agenda_definition = (
            select id from mv_agenda_definition where id_attribute in (
                select id from attribute where lookup = 'lookup_judo_categories'
            )
        ) and value in (select id::varchar from lookup_judo_categories where name in ('2023_mu16_-38 kg', '2023_mu16_+38 kg'))
    )
);

update lookup_weightcategories set enabled = false WHERE id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu16') AND name = '-38 kg';
update lookup_weightcategories set enabled = false WHERE id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_mu16') AND name = '+38 kg';

REFRESH MATERIALIZED VIEW lookup_judo_categories;

-- wu14 remove -30 kg & +30 kg

UPDATE lookup_weightcategories set weight_from = 0
WHERE id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu14')
AND name = '-33 kg';

-- migrate old values --
-- PROD version --
update agenda_has_agenda_values set value = (select id::varchar from lookup_judo_categories where name in ('2023_wu14_-33 kg')) where id in (
     select id from agenda_has_agenda_values where id_agenda_entity_has_agenda_entity in (
        select id from agenda_entity_has_agenda_entity
          where id_agenda_entity_linked in (
              select distinct(id_agenda_entity) from agenda_values where id_agenda_definition = (
                select id from mv_agenda_definition where id_attribute in (
                    select id from attribute where lookup = 'lookup_judo_categories'
                )
              ) and value in (select id::varchar from lookup_judo_categories where name in ('2023_wu14_-30 kg', '2023_wu14_+30 kg'))
          )
          and id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'tournaments_has_members')
    ) and id_agenda_has_agenda_definition = (
        select id from agenda_has_agenda_definition
             where id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'tournaments_has_members')
                and id_attribute = (select id from attribute where name = 'weight_category')
    ) and value in (select id::varchar from lookup_judo_categories where name in ('2023_wu14_-30 kg', '2023_wu14_+30 kg'))
);

-- delete agenda_values removed category + date from/to
delete from agenda_values where id in (
    select id from agenda_values where (id_agenda_entity, recurring_sequence) in (
         select id_agenda_entity, recurring_sequence from agenda_values where id_agenda_definition = (
            select id from mv_agenda_definition where id_attribute in (
                select id from attribute where lookup = 'lookup_judo_categories'
            )
        ) and value in (select id::varchar from lookup_judo_categories where name in ('2023_wu14_-30 kg', '2023_wu14_+30 kg'))
    )
);

update lookup_weightcategories set enabled = false WHERE id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu14') AND name = '-30 kg';
update lookup_weightcategories set enabled = false WHERE id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu14') AND name = '+30 kg';

REFRESH MATERIALIZED VIEW lookup_judo_categories;


-- wu16 remove -36 kg & +36 kg & -78 kg & +78 kg

UPDATE lookup_weightcategories set weight_from = 0
WHERE id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu16')
AND name = '-40 kg';

UPDATE lookup_weightcategories set id_lookup_weightcategories_plus = null
WHERE id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu16')
AND name = '-70 kg';

UPDATE lookup_weightcategories set is_hidden = false
WHERE id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu16')
AND name = '+70 kg';

-- migrate old values to -40 --
-- PROD version --
update agenda_has_agenda_values set value = (select id::varchar from lookup_judo_categories where name in ('2023_wu16_-40 kg')) where id in (
     select id from agenda_has_agenda_values where id_agenda_entity_has_agenda_entity in (
        select id from agenda_entity_has_agenda_entity
          where id_agenda_entity_linked in (
              select distinct(id_agenda_entity) from agenda_values where id_agenda_definition = (
                select id from mv_agenda_definition where id_attribute in (
                    select id from attribute where lookup = 'lookup_judo_categories'
                )
              ) and value in (select id::varchar from lookup_judo_categories where name in ('2023_wu16_-36 kg', '2023_wu16_+36 kg'))
          )
          and id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'tournaments_has_members')
    ) and id_agenda_has_agenda_definition = (
        select id from agenda_has_agenda_definition
             where id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'tournaments_has_members')
                and id_attribute = (select id from attribute where name = 'weight_category')
    ) and value in (select id::varchar from lookup_judo_categories where name in ('2023_wu16_-36 kg', '2023_wu16_+36 kg'))
);

-- migrate old values to +70 --
-- PROD version --
update agenda_has_agenda_values set value = (select id::varchar from lookup_judo_categories where name in ('2023_wu16_+70 kg')) where id in (
     select id from agenda_has_agenda_values where id_agenda_entity_has_agenda_entity in (
        select id from agenda_entity_has_agenda_entity
          where id_agenda_entity_linked in (
              select distinct(id_agenda_entity) from agenda_values where id_agenda_definition = (
                select id from mv_agenda_definition where id_attribute in (
                    select id from attribute where lookup = 'lookup_judo_categories'
                )
              ) and value in (select id::varchar from lookup_judo_categories where name in ('2023_wu16_-78 kg', '2023_wu16_+78 kg'))
          )
          and id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'tournaments_has_members')
    ) and id_agenda_has_agenda_definition = (
        select id from agenda_has_agenda_definition
             where id_agenda_has_agenda = (select id from agenda_has_agenda where name = 'tournaments_has_members')
                and id_attribute = (select id from attribute where name = 'weight_category')
    ) and value in (select id::varchar from lookup_judo_categories where name in ('2023_wu16_-78 kg', '2023_wu16_+78 kg'))
);

-- delete agenda_values removed category + date from/to
delete from agenda_values where id in (
    select id from agenda_values where (id_agenda_entity, recurring_sequence) in (
         select id_agenda_entity, recurring_sequence from agenda_values where id_agenda_definition = (
            select id from mv_agenda_definition where id_attribute in (
                select id from attribute where lookup = 'lookup_judo_categories'
            )
        ) and value in (select id::varchar from lookup_judo_categories where name in ('2023_wu16_-36 kg', '2023_wu16_+36 kg', '2023_wu16_-78 kg'))
    )
);


-- change value in recurring group to max weight in category
update agenda_values set value = (select id::varchar from lookup_judo_categories where name in ('2023_wu16_+70 kg')) where id in (
    select id from agenda_values where id_agenda_definition = (
        select id from mv_agenda_definition where id_attribute in (
            select id from attribute where lookup = 'lookup_judo_categories'
        )
    ) and value in (select id::varchar from lookup_judo_categories where name in ('2023_wu16_+78 kg'))
);

update lookup_weightcategories set enabled = false WHERE id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu16') AND name = '-36 kg';
update lookup_weightcategories set enabled = false WHERE id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu16') AND name = '+36 kg';
update lookup_weightcategories set enabled = false WHERE id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu16') AND name = '-78 kg';
update lookup_weightcategories set enabled = false WHERE id_lookup_categories = (SELECT id FROM lookup_categories WHERE name = '2023_wu16') AND name = '+78 kg';6

REFRESH MATERIALIZED VIEW lookup_judo_categories;

--- categories update name ---

UPDATE lookup_categories SET name = '2024_u8', valid_from = '2024', year_from = '2017', year_to = '2017' WHERE name = '2023_u8';
UPDATE lookup_categories SET name = '2024_u10', valid_from = '2024', year_from = '2015', year_to = '2016' WHERE name = '2023_u10';
UPDATE lookup_categories SET name = '2024_mu12', valid_from = '2024', year_from = '2013', year_to = '2014' WHERE name = '2023_mu12';
UPDATE lookup_categories SET name = '2024_mu14', valid_from = '2024', year_from = '2011', year_to = '2012' WHERE name = '2023_mu14';
UPDATE lookup_categories SET name = '2024_mu16', valid_from = '2024', year_from = '2009', year_to = '2010' WHERE name = '2023_mu16';
UPDATE lookup_categories SET name = '2024_mu18', valid_from = '2024', year_from = '2007', year_to = '2008' WHERE name = '2023_mu18';
UPDATE lookup_categories SET name = '2024_mu21', valid_from = '2024', year_from = '2004', year_to = '2006' WHERE name = '2023_mu21';
UPDATE lookup_categories SET name = '2024_muzi', valid_from = '2024', year_from = '2003', year_to = '2103' WHERE name = '2023_muzi';
UPDATE lookup_categories SET name = '2024_wu12', valid_from = '2024', year_from = '2013', year_to = '2014' WHERE name = '2023_wu12';
UPDATE lookup_categories SET name = '2024_wu14', valid_from = '2024', year_from = '2011', year_to = '2012' WHERE name = '2023_wu14';
UPDATE lookup_categories SET name = '2024_wu16', valid_from = '2024', year_from = '2009', year_to = '2010' WHERE name = '2023_wu16';
UPDATE lookup_categories SET name = '2024_wu18', valid_from = '2024', year_from = '2007', year_to = '2008' WHERE name = '2023_wu18';
UPDATE lookup_categories SET name = '2024_wu21', valid_from = '2024', year_from = '2004', year_to = '2006' WHERE name = '2023_wu21';
UPDATE lookup_categories SET name = '2024_zeny', valid_from = '2024', year_from = '2003', year_to = '2103' WHERE name = '2023_zeny';
UPDATE lookup_categories SET name = '2024_mu23', valid_from = '2024', year_from = '2002', year_to = '2006' WHERE name = '2023_mu23';
UPDATE lookup_categories SET name = '2024_wu23', valid_from = '2024', year_from = '2002', year_to = '2006' WHERE name = '2023_wu23';

REFRESH MATERIALIZED VIEW lookup_judo_categories;

-- #14259
UPDATE flexii.user_group_has_definition_value_for_aha SET is_primary = true WHERE name = 'member_has_club_and_admin_lvl1';



-- #14220
UPDATE agenda_definition
SET is_disabled = true
WHERE id = (SELECT id
            FROM mv_agenda_definition
            WHERE agenda_name = 'union_fees'
              AND segment_name = 'union_fees_main'
              AND attribute_group_name = 'union_fees_main'
              AND attribute_name = 'invoice');



/*SCRIPT FOR INVOICE COUNT*/


/*
SELECT COUNT(*) as num_of_invoices
FROM agenda_has_file
WHERE id_agenda_definition
          = (select id
             from mv_agenda_definition
             where agenda_name = 'union_fees'
               and segment_name = 'union_fees_main'
               and attribute_group_name = 'union_fees_main'
               and attribute_name = 'invoice');
*/

/* TODO: !!!!!!!!!!!!! CHANGE VALUE OF SEQUNCE BASED ON CURRENT PRODUCTION !!!!!!!!!!!!!*/
CREATE SEQUENCE flexii.seq_invoice_count
    INCREMENT BY 1
    MINVALUE 0
    MAXVALUE 999999
    START WITH 1 -- <- CHANGE THIS
    CACHE 1
    NO CYCLE
    OWNED BY NONE;


UPDATE agenda_button
SET confirmation = 'Tato akce je nevratná, jste si jisti?'
WHERE name = 'UnionFeePayment';

INSERT INTO ATTRIBUTE
(NAME, DESCRIPTION, DATA_TYPE_PRECISION, LOOKUP, LOOKUP_COLUMNS, WEIGHT, id_attribute_data_type)
VALUES ('invoice_sequence', 'Č. sekvence faktury', 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 = 'invoice_sequence')
      , 100, false, NULL, false, true, NULL, 3, 0, 1, FALSE, FALSE, null
      , (select id from agenda_has_segment_has_attribute_group_definition
         where id_agenda_has_segment_definition = (select id from agenda_has_segment_definition where id_agenda = (select id from agenda where name = 'union_fees')
                                                                                                  and id_agenda_segment = (select id from agenda_segment where name = 'union_fees_main')
         )
           and id_agenda_attribute_group= (select id from agenda_attribute_group where name = 'union_fees_main')
          ), true
      );



INSERT INTO agenda_definition_specification_by_value
(id_agenda_definition, id_agenda_definition_control, is_hidden, is_value_control_filled)
VALUES ((SELECT id
         FROM agenda_definition
         WHERE enabled = true
           AND id_attribute = (SELECT id FROM attribute WHERE name = 'invoice_sequence')
           AND id_agenda_segment_attribute_group_definition = (select id
                                                               from agenda_has_segment_has_attribute_group_definition
                                                               where id_agenda_has_segment_definition = (select id
                                                                                                         from agenda_has_segment_definition
                                                                                                         where id_agenda = (select id from agenda where name = 'union_fees')
                                                                                                           and id_agenda_segment =
                                                                                                               (select id from agenda_segment where name = 'union_fees_main'))
                                                                 and id_agenda_attribute_group =
                                                                     (select id from agenda_attribute_group where name = 'union_fees_main')))
           ,
        (SELECT id
         FROM agenda_definition
         WHERE enabled = true
           AND id_attribute = (SELECT id FROM attribute WHERE name = 'year_fee')
           AND id_agenda_segment_attribute_group_definition = (select id
                                                               from agenda_has_segment_has_attribute_group_definition
                                                               where id_agenda_has_segment_definition = (select id
                                                                                                         from agenda_has_segment_definition
                                                                                                         where id_agenda = (select id from agenda where name = 'union_fees')
                                                                                                           and id_agenda_segment =
                                                                                                               (select id from agenda_segment where name = 'union_fees_main'))
                                                                 and id_agenda_attribute_group =
                                                                     (select id from agenda_attribute_group where name = 'union_fees_main') )),true,true);
REFRESH MATERIALIZED VIEW mv_agenda_definition;

update user_group_has_agenda_has_agenda set is_can_read = true, is_can_write = false, is_can_create = false, is_can_delete = false
    where id_user_group = (SELECT id FROM user_group WHERE name = 'Clenove_Vse_Cteni+EditaceKyu')
    and  id_agenda_has_agenda = (SELECT id FROM agenda_has_agenda WHERE name = 'clubs_has_members');

update user_group_has_agenda_has_agenda set is_can_read = true, is_can_write = false, is_can_create = false, is_can_delete = false
    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');

REFRESH MATERIALIZED VIEW mv_user_group_has_agenda_has_agenda;

----------------------------------------------------
-- 2.6.7.2
-- SQL for this version ends here
----------------------------------------------------

-- #14276
UPDATE flexii.agenda_has_agenda SET is_limited_by_permission = true WHERE name = 'tournaments_has_members';

-- #14309
DELETE
FROM agenda_values
    WHERE id_agenda_definition IN (SELECT id
                                     FROM mv_agenda_definition
                                     WHERE agenda_name = 'union_fees'
                                       AND segment_name = 'union_fees_main'
                                       AND attribute_group_name = 'union_fees_main'
                                       AND attribute_name = 'invoice_sequence');


DELETE
FROM agenda_definition_specification_by_value
WHERE id_agenda_definition IN (SELECT id
                               FROM mv_agenda_definition
                               WHERE agenda_name = 'union_fees'
                                 AND segment_name = 'union_fees_main'
                                 AND attribute_group_name = 'union_fees_main'
                                 AND attribute_name = 'invoice_sequence')

  AND id_agenda_definition_control IN (SELECT id
                                       FROM mv_agenda_definition
                                       WHERE agenda_name = 'union_fees'
                                         AND segment_name = 'union_fees_main'
                                         AND attribute_group_name = 'union_fees_main'
                                         AND attribute_name = 'year_fee');

DELETE
FROM agenda_definition
WHERE id IN (SELECT id
             FROM mv_agenda_definition
             WHERE agenda_name = 'union_fees'
               AND segment_name = 'union_fees_main'
               AND attribute_group_name = 'union_fees_main'
               AND attribute_name = 'invoice_sequence');

DELETE FROM attribute WHERE name = 'invoice_sequence';


DROP sequence flexii.seq_invoice_count;

----------------------------------------------------
-- 2.6.8.0
-- SQL for this version ends here
----------------------------------------------------

-- #14329
UPDATE flexii.agenda_has_agenda SET is_editable_agenda = false WHERE name = 'tournaments_has_members';

----------------------------------------------------
-- 2.6.8.2
-- SQL for this version ends here
----------------------------------------------------

-- #14377
UPDATE base.user_group SET description = 'Členové - členové klubu admina - limitovaná editace + přidání' WHERE name = 'Clenove_ClenoveKlubuAdmina_EditLimitovan+Pridani';
UPDATE base.user_group SET description = 'Kluby - adminem klubu - limitovaná editace + objednani poplatku + tisk karet' WHERE name = 'Kluby_AdminemKlubu_EditLimitovan+ObjednaniPoplatku+TiskKaret';
UPDATE base.user_group SET description = 'Rozhodčí - vše - limitovaná editace' WHERE name = 'Rozhodci_Vse_EditLimitovan';
UPDATE base.user_group SET description = 'Turnaje - turnaj aktivni nebo pořádán klubem Admina - čtení + přihlášení členů svého klubu' WHERE name = 'Turnaje_TurnajAktivniAPoradanKlubemAdmina_Cteni+PrihlaseniClenuSvehoKlubu';

----------------------------------------------------
-- 2.6.8.4
-- SQL for this version ends here
----------------------------------------------------

-- #14394
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_Cteni+EditaceKyu'), (SELECT id FROM agenda WHERE name = 'examiner'), 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_has_examiner_lvl_1', null, (SELECT id FROM base.user_group WHERE name = 'Clenove_Vse_Cteni+EditaceKyu'), (SELECT id FROM agenda WHERE name = 'examiner'), (SELECT id FROM agenda_has_agenda WHERE name = 'members_has_examiner'), (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') , null)
;


-- #14187
INSERT INTO agenda_definition_custom (id_agenda_definition, class_name) VALUES ((SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'members-members_main-members_main-clubs_has_members'), 'MemberClub');

----------------------------------------------------
-- 2.6.9.0
-- SQL for this version ends here
----------------------------------------------------

-- #14407
INSERT INTO flexii.definition_table_value (id_definition_key, id_definition, value) VALUES ((SELECT id FROM definition_key WHERE name = 'is_agenda_lookup_restricted_by_permission' AND table_name = 'agenda_definition'), (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'referee-referee_class_main-referee_class_main-agenda_official'), FALSE);
INSERT INTO flexii.definition_table_value (id_definition_key, id_definition, value) VALUES ((SELECT id FROM definition_key WHERE name = 'is_agenda_lookup_restricted_by_permission' AND table_name = 'agenda_definition'), (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'referee-international_referee_class_main-international_referee_class_main-agenda_official'), FALSE);
INSERT INTO flexii.definition_table_value (id_definition_key, id_definition, value) VALUES ((SELECT id FROM definition_key WHERE name = 'is_agenda_lookup_restricted_by_permission' AND table_name = 'agenda_definition'), (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'referee-instructor_main-instructor_main-agenda_official'), FALSE);

REFRESH MATERIALIZED VIEW mv_agenda_definition_property;

----------------------------------------------------
-- 2.6.9.3
-- SQL for this version ends here
----------------------------------------------------

-- #14359
update agenda_definition set is_not_null = true
where id = (
SELECT id FROM mv_agenda_definition
 WHERE agenda_name = 'official'
   AND segment_name = 'official_main'
   AND attribute_group_name = 'official_main'
   AND attribute_name = 'members_has_official'
);

refresh materialized view mv_agenda_definition;

-- #14241

INSERT INTO agenda_has_agenda_show_attribute (id_agenda_has_agenda, id_agenda_definition, is_hidden_in_datagrid)
VALUES ( (SELECT id from agenda_has_agenda where name = 'tournaments_has_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);

INSERT INTO agenda_has_agenda_show_attribute (id_agenda_has_agenda, id_agenda_definition, is_hidden_in_datagrid, enabled)
VALUES ( (SELECT id from agenda_has_agenda where name = 'tournaments_has_members'),
         (SELECT id from mv_agenda_definition
          WHERE agenda_name = 'members'
            AND segment_name = 'members_main'
            AND attribute_group_name ='members_contact_main'
            AND attribute_name = 'phone_office'
         ), false, false);

----------------------------------------------------
-- 2.6.10.0
-- SQL for this version ends here
----------------------------------------------------

-- #14427
UPDATE flexii.agenda_has_agenda_show_attribute_group SET is_limited_by_permission = false WHERE id IN (
    SELECT id FROM agenda_has_agenda_show_attribute_group WHERE id_agenda_has_agenda IN (SELECT id FROM agenda_has_agenda WHERE name IN ('members_has_referee', 'members_has_trainer', 'members_has_examiner'))
    );

----------------------------------------------------
-- 2.6.10.1
-- SQL for this version ends here
----------------------------------------------------

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_ClenoveKlubuAdmina_EditLimitovan+Pridani'), (select id from agenda where name = 'competitions'), true, false, false, false);

----------------------------------------------------
-- 2.6.10.2
-- SQL for this version ends here
----------------------------------------------------

UPDATE attribute SET id_attribute_data_type = (SELECT id FROM attribute_data_type WHERE name = 'file') WHERE name = 'proposition';
REFRESH MATERIALIZED VIEW mv_agenda_definition;


-- #14493
UPDATE user_group_has_agenda
SET is_can_create = TRUE
WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'trainer')
  AND id_user_group IN (SELECT id
                        FROM user_group
                        WHERE name IN
                              (
                               'Treneri_ClenKlubuJihoceskyKraj_EditaceTridy+Bodu_Pouze3.Trida',
                               'Treneri_ClenKlubuJihomoravskyKraj_EditaceTridy+Bodu_Pouze3.Trida',
                               'Treneri_ClenKlubuKarlovarskyKraj_EditaceTridy+Bodu_Pouze3.Trida',
                               'Treneri_ClenKlubuKralovehradeckyKraj_EditaceTridy+Bodu_Pouze3.Trida',
                               'Treneri_ClenKlubuLibereckyKraj_EditaceTridy+Bodu_Pouze3.Trida',
                               'Treneri_ClenKlubuMoravskoslezskyKraj_EditaceTridy+Bodu_Pouze3.Trida',
                               'Treneri_ClenKlubuOlomouckyKraj_EditaceTridy+Bodu_Pouze3.Trida',
                               'Treneri_ClenKlubuPardubickyKraj_EditaceTridy+Bodu_Pouze3.Trida',
                               'Treneri_ClenKlubuPlzenskyKraj_EditaceTridy+Bodu_Pouze3.Trida',
                               'Treneri_ClenKlubuPraha_EditaceTridy+Bodu_Pouze3.Trida',
                               'Treneri_ClenKlubuStredoceskyKraj_EditaceTridy+Bodu_Pouze3.Trida',
                               'Treneri_ClenKlubuUsteckyKraj_EditaceTridy+Bodu_Pouze3.Trida',
                               'Treneri_ClenKlubuVysocina_EditaceTridy+Bodu_Pouze3.Trida',
                               'Treneri_ClenKlubuZlinskyKraj_EditaceTridy+Bodu_Pouze3.Trida'
                                  ));

DELETE FROM user_group_has_agenda_has_segment_definition WHERE id_user_group IN (SELECT id
      FROM user_group
      WHERE name IN
            (
             'Treneri_ClenKlubuJihoceskyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuJihomoravskyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuKarlovarskyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuKralovehradeckyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuLibereckyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuMoravskoslezskyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuOlomouckyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuPardubickyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuPlzenskyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuPraha_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuStredoceskyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuUsteckyKraj_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuVysocina_EditaceTridy+Bodu_Pouze3.Trida',
             'Treneri_ClenKlubuZlinskyKraj_EditaceTridy+Bodu_Pouze3.Trida'
                ))
 AND id_agenda_has_segment_definition IN (SELECT DISTINCT id_agenda_has_segment_definition FROM mv_agenda_definition WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'trainer'));

UPDATE user_group_has_agenda
SET is_can_create = TRUE
WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'referee')
  AND id_user_group IN (SELECT id
                        FROM user_group
                        WHERE name IN
                              (
                               'Rozhodci_ClenKlubuJihoceskyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuJihomoravskyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuKarlovarskyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuKralovehradeckyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuLibereckyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuMoravskoslezskyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuOlomouckyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuPardubickyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuPlzenskyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuPraha_Edit3.Trida',
                               'Rozhodci_ClenKlubuStredoceskyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuUsteckyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuVysocina_Edit3.Trida',
                               'Rozhodci_ClenKlubuZlinskyKraj_Edit3.Trida'
                                  ));

DELETE
FROM user_group_has_agenda_has_segment_definition
WHERE id_user_group IN (SELECT id
                        FROM user_group
                        WHERE name IN
                              (
                               'Rozhodci_ClenKlubuJihoceskyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuJihomoravskyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuKarlovarskyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuKralovehradeckyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuLibereckyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuMoravskoslezskyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuOlomouckyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuPardubickyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuPlzenskyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuPraha_Edit3.Trida',
                               'Rozhodci_ClenKlubuStredoceskyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuUsteckyKraj_Edit3.Trida',
                               'Rozhodci_ClenKlubuVysocina_Edit3.Trida',
                               'Rozhodci_ClenKlubuZlinskyKraj_Edit3.Trida'
                                  ))
  AND id_agenda_has_segment_definition IN
      (SELECT DISTINCT id_agenda_has_segment_definition FROM mv_agenda_definition WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'referee'));


UPDATE agenda_definition
SET is_not_null = FALSE
WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'referee-referee_main-referee_main-appuser');

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, true AS is_read, false AS is_write
FROM
    (SELECT id
     FROM user_group
     WHERE name IN
           (
            'Rozhodci_ClenKlubuJihoceskyKraj_Edit3.Trida',
            'Rozhodci_ClenKlubuJihomoravskyKraj_Edit3.Trida',
            'Rozhodci_ClenKlubuKarlovarskyKraj_Edit3.Trida',
            'Rozhodci_ClenKlubuKralovehradeckyKraj_Edit3.Trida',
            'Rozhodci_ClenKlubuLibereckyKraj_Edit3.Trida',
            'Rozhodci_ClenKlubuMoravskoslezskyKraj_Edit3.Trida',
            'Rozhodci_ClenKlubuOlomouckyKraj_Edit3.Trida',
            'Rozhodci_ClenKlubuPardubickyKraj_Edit3.Trida',
            'Rozhodci_ClenKlubuPlzenskyKraj_Edit3.Trida',
            'Rozhodci_ClenKlubuPraha_Edit3.Trida',
            'Rozhodci_ClenKlubuStredoceskyKraj_Edit3.Trida',
            'Rozhodci_ClenKlubuUsteckyKraj_Edit3.Trida',
            'Rozhodci_ClenKlubuVysocina_Edit3.Trida',
            'Rozhodci_ClenKlubuZlinskyKraj_Edit3.Trida'
               )) grp, (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'referee-referee_main-referee_main-appuser') ad;


UPDATE user_group_has_agenda
SET is_can_create = TRUE
WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'examiner')
  AND id_user_group IN (SELECT id
                        FROM user_group
                        WHERE name IN
                              (
                               'ZkusebniKomisari_ClenKlubuJihoceskyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuJihomoravskyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuKarlovarskyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuKralovehradeckyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuLibereckyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuMoravskoslezskyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuOlomouckyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuPardubickyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuPlzenskyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuPraha_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuStredoceskyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuUsteckyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuVysocina_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuZlinskyKraj_EditaceTrid+Pridani3.Trida'
                                  ));

DELETE
FROM user_group_has_agenda_has_segment_definition
WHERE id_user_group IN (SELECT id
                        FROM user_group
                        WHERE name IN
                              (
                               'ZkusebniKomisari_ClenKlubuJihoceskyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuJihomoravskyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuKarlovarskyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuKralovehradeckyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuLibereckyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuMoravskoslezskyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuOlomouckyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuPardubickyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuPlzenskyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuPraha_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuStredoceskyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuUsteckyKraj_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuVysocina_EditaceTrid+Pridani3.Trida',
                               'ZkusebniKomisari_ClenKlubuZlinskyKraj_EditaceTrid+Pridani3.Trida'
                                  ))
  AND id_agenda_has_segment_definition IN
      (SELECT DISTINCT id_agenda_has_segment_definition FROM mv_agenda_definition WHERE id_agenda = (SELECT id FROM agenda WHERE name = 'examiner'));

UPDATE agenda_definition
SET is_not_null = FALSE
WHERE id = (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'examiner-examiner_main-examiner_main-appuser');

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, true AS is_read, false AS is_write
FROM
(SELECT id
FROM user_group
WHERE name IN
      (
       'ZkusebniKomisari_ClenKlubuJihoceskyKraj_EditaceTrid+Pridani3.Trida',
       'ZkusebniKomisari_ClenKlubuJihomoravskyKraj_EditaceTrid+Pridani3.Trida',
       'ZkusebniKomisari_ClenKlubuKarlovarskyKraj_EditaceTrid+Pridani3.Trida',
       'ZkusebniKomisari_ClenKlubuKralovehradeckyKraj_EditaceTrid+Pridani3.Trida',
       'ZkusebniKomisari_ClenKlubuLibereckyKraj_EditaceTrid+Pridani3.Trida',
       'ZkusebniKomisari_ClenKlubuMoravskoslezskyKraj_EditaceTrid+Pridani3.Trida',
       'ZkusebniKomisari_ClenKlubuOlomouckyKraj_EditaceTrid+Pridani3.Trida',
       'ZkusebniKomisari_ClenKlubuPardubickyKraj_EditaceTrid+Pridani3.Trida',
       'ZkusebniKomisari_ClenKlubuPlzenskyKraj_EditaceTrid+Pridani3.Trida',
       'ZkusebniKomisari_ClenKlubuPraha_EditaceTrid+Pridani3.Trida',
       'ZkusebniKomisari_ClenKlubuStredoceskyKraj_EditaceTrid+Pridani3.Trida',
       'ZkusebniKomisari_ClenKlubuUsteckyKraj_EditaceTrid+Pridani3.Trida',
       'ZkusebniKomisari_ClenKlubuVysocina_EditaceTrid+Pridani3.Trida',
       'ZkusebniKomisari_ClenKlubuZlinskyKraj_EditaceTrid+Pridani3.Trida'
          )) grp, (SELECT id FROM mv_agenda_definition WHERE full_unique_name = 'examiner-examiner_main-examiner_main-appuser') ad;

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;


-- #14459 --
