create table app_user
(
    email_on_comment_added  boolean                     not null,
    email_on_form_created   boolean                     not null,
    email_on_form_submitted boolean                     not null,
    email_on_form_updated   boolean                     not null,
    created_at              timestamp(6) with time zone not null,
    modified_at             timestamp(6) with time zone not null,
    email                   varchar(255),
    keycloak_id             varchar(255)                not null,
    name                    varchar(255)                not null,
    organization_id         varchar(255),
    primary key (keycloak_id)
);

create table application_form
(
    is_template         boolean                     not null,
    created_at          timestamp(6) with time zone not null,
    modified_at         timestamp(6) with time zone not null,
    id                  uuid                        not null,
    created_by_id       varchar(255)                not null,
    last_modified_by_id varchar(255)                not null,
    name                varchar(255)                not null,
    organization_id     varchar(255),
    status              varchar(255)                not null check (status in ('DRAFT', 'SUBMITTED', 'APPROVED', 'REJECTED', 'SIGNED')),
    primary key (id)
);

create table application_form_version
(
    version_number      integer                     not null,
    created_at          timestamp(6) with time zone not null,
    application_form_id uuid                        not null,
    id                  uuid                        not null,
    created_by_id       varchar(255)                not null,
    name                varchar(255)                not null,
    organization_id     varchar(255)                not null,
    snapshot_data       TEXT                        not null,
    status              varchar(255)                not null check (status in ('DRAFT', 'SUBMITTED', 'APPROVED', 'REJECTED', 'SIGNED')),
    primary key (id)
);

create table comment
(
    created_at          timestamp(6) with time zone not null,
    modified_at         timestamp(6) with time zone not null,
    application_form_id uuid                        not null,
    form_element_id     uuid                        not null,
    id                  uuid                        not null,
    created_by_id       varchar(255)                not null,
    message             TEXT                        not null,
    primary key (id)
);

create table form_element_options
(
    col_config_filter_src_col_idx               integer,
    col_config_is_checkbox                      boolean,
    col_config_is_read_only                     boolean,
    col_config_source_col_idx                   integer,
    employee_data_category                      smallint     not null check (employee_data_category between 0 and 3),
    is_multiple_allowed                         boolean,
    processing_purpose                          smallint     not null check (processing_purpose between 0 and 3),
    row_constraint_current_row_key_column_index integer,
    row_constraint_key_column_index             integer,
    row_constraint_value_column_index           integer,
    form_element_id                             uuid         not null,
    col_config_filter_expected_val              varchar(255),
    col_config_filter_operator                  varchar(255) check (col_config_filter_operator in
                                                                    ('EQUALS', 'NOT_EQUALS', 'IS_EMPTY',
                                                                     'IS_NOT_EMPTY')),
    col_config_source_table_ref                 varchar(255),
    label                                       varchar(255) not null,
    option_value                                TEXT         not null,
    row_constraint_table_reference              varchar(255)
);

create table form_element
(
    can_add_rows                   boolean,
    form_element_order             integer,
    is_clonable                    boolean  not null,
    row_preset_filter_src_col_idx  integer,
    type                           smallint not null check (type between 0 and 8),
    form_element_sub_section_id    uuid     not null,
    id                             uuid     not null,
    description                    varchar(255),
    reference                      varchar(255),
    row_preset_filter_expected_val varchar(255),
    row_preset_filter_operator     varchar(255) check (row_preset_filter_operator in
                                                       ('EQUALS', 'NOT_EQUALS', 'IS_EMPTY', 'IS_NOT_EMPTY')),
    row_preset_source_table_ref    varchar(255),
    title                          varchar(255),
    primary key (id)
);

create table form_element_section
(
    is_template                    boolean      not null,
    application_form_id            uuid         not null,
    id                             uuid         not null,
    description                    varchar(255),
    short_title                    varchar(255),
    spawned_from_element_reference varchar(255),
    template_reference             varchar(255),
    title                          varchar(255) not null,
    title_template                 varchar(255),
    primary key (id)
);

create table form_element_sub_section
(
    form_element_sub_section_order integer,
    form_element_section_id        uuid         not null,
    id                             uuid         not null,
    subtitle                       varchar(255),
    title                          varchar(255) not null,
    primary key (id)
);

create table notification
(
    is_read          boolean                     not null,
    created_at       timestamp(6) with time zone not null,
    id               uuid                        not null,
    click_target     varchar(255)                not null,
    excluded_user_id varchar(255),
    message          TEXT                        not null,
    organization_id  varchar(255)                not null,
    recipient_id     varchar(255),
    target_roles     TEXT,
    title            varchar(255)                not null,
    type             varchar(255)                not null check (type in ('INFO', 'WARNING', 'ERROR')),
    primary key (id)
);

create table section_spawn_triggers
(
    form_element_id              uuid not null,
    section_spawn_condition_type varchar(255) check (section_spawn_condition_type in ('SHOW', 'HIDE')),
    section_spawn_expected_value varchar(255),
    section_spawn_operator       varchar(255) check (section_spawn_operator in
                                                     ('EQUALS', 'NOT_EQUALS', 'IS_EMPTY', 'IS_NOT_EMPTY')),
    template_reference           varchar(255)
);

create table table_column_mappings
(
    source_column_index integer,
    target_column_index integer,
    form_element_id     uuid not null
);

create table visibility_conditions
(
    form_element_id               uuid not null,
    form_element_condition_type   varchar(255) check (form_element_condition_type in ('SHOW', 'HIDE')),
    form_element_expected_value   varchar(255),
    form_element_operator         varchar(255) check (form_element_operator in
                                                      ('EQUALS', 'NOT_EQUALS', 'IS_EMPTY', 'IS_NOT_EMPTY')),
    source_form_element_reference varchar(255)
);

alter table if exists application_form
    add constraint FKhtad5onoy2jknhtyfmx6cvvey
    foreign key (created_by_id)
    references app_user;

alter table if exists application_form
    add constraint FK5yewx8bespw0uiivxioeh7q0d
    foreign key (last_modified_by_id)
    references app_user;

alter table if exists application_form_version
    add constraint FKpfri4lhy9wqfsp8esabedkq6c
    foreign key (application_form_id)
    references application_form
    on delete cascade;

alter table if exists application_form_version
    add constraint FKl6fbcrvh439gbwgcvvfyxaggi
    foreign key (created_by_id)
    references app_user;

alter table if exists comment
    add constraint FKlavy9axrt26sepreg5lqtuoap
    foreign key (application_form_id)
    references application_form;

alter table if exists comment
    add constraint FKbbjqikfmgeacfsnaasxxqoygh
    foreign key (created_by_id)
    references app_user;

alter table if exists comment
    add constraint FKfg84w0i76tw9os13950272c6f
    foreign key (form_element_id)
    references form_element;

alter table if exists form_element_options
    add constraint FKnq0lpby5nspv1xi27n9el6us6
    foreign key (form_element_id)
    references form_element;

alter table if exists form_element
    add constraint FKsgs6jmigb70ylymtby3tnpusg
    foreign key (form_element_sub_section_id)
    references form_element_sub_section;

alter table if exists form_element_section
    add constraint FKtn0lreovauwf2v29doo70o3qs
    foreign key (application_form_id)
    references application_form;

alter table if exists form_element_sub_section
    add constraint FK46c23gs3wjyyitiv77wfbq2id
    foreign key (form_element_section_id)
    references form_element_section;

alter table if exists notification
    add constraint FKeg1j4hnp0y4lbm0y35hgr4e8r
    foreign key (recipient_id)
    references app_user;

alter table if exists section_spawn_triggers
    add constraint FK7lf0hf8cepm2o9nty147x2ahm
    foreign key (form_element_id)
    references form_element;

alter table if exists table_column_mappings
    add constraint FK2t3a4fl5kqtqky39r7boqegf9
    foreign key (form_element_id)
    references form_element;

alter table if exists visibility_conditions
    add constraint FK5xuf7bd179ogpq5a1m3g8q7jb
    foreign key (form_element_id)
    references form_element;
