create table app_user
(
    created_at  timestamp(6) not null,
    modified_at timestamp(6) not null,
    id          varchar(255) not null,
    name        varchar(255) not null,
    status      varchar(255) not null check (status in ('INVITED', 'ACTIVE', 'BLOCKED', 'SUSPENDED_SUBSCRIPTION')),
    primary key (id)
);

create table application_form
(
    is_template         boolean      not null,
    created_at          timestamp(6) not null,
    modified_at         timestamp(6) 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 comment
(
    created_at          timestamp(6) not null,
    modified_at         timestamp(6) 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             varchar(255) not null,
    primary key (id)
);

create table form_element_options
(
    employee_data_category smallint     not null check (employee_data_category between 0 and 3),
    processing_purpose     smallint     not null check (processing_purpose between 0 and 3),
    form_element_id        uuid         not null,
    label                  varchar(255) not null,
    option_value           varchar(255) not null
);

create table form_element
(
    type                    smallint not null check (type between 0 and 4),
    form_element_section_id uuid     not null,
    id                      uuid     not null,
    description             varchar(255),
    title                   varchar(255),
    primary key (id)
);

create table form_element_section
(
    application_form_id uuid         not null,
    id                  uuid         not null,
    description         varchar(255),
    short_title         varchar(255),
    title               varchar(255) not null,
    primary key (id)
);

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

create table user_organization_roles
(
    organization_id varchar(255) not null,
    role            varchar(255) not null,
    user_id         varchar(255) not null,
    primary key (organization_id, role, user_id)
);

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 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 FKdpr6k93m4hqllqjsvoa4or6mp
    foreign key (form_element_section_id)
    references form_element_section;

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

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

alter table if exists user_organization_roles
    add constraint FKhgmm93qre3up6hy63wcef3yqk
    foreign key (user_id)
    references app_user
