create table xb_kwglobal_config_groups
(
    rights_package varchar(32) not null,
    group_rights   varchar(32) not null,
    primary key (rights_package, group_rights)
)
    charset = utf8mb4;

create table xb_kwglobal_config_sessions
(
    user_id   int unsigned                                                         not null,
    user_type enum ('admin', 'employee', 'business', 'stripe', 'invoice', 'sales') not null,
    sessions  int unsigned default 3                                               not null,
    primary key (user_id, user_type)
)
    charset = utf8;

create table xb_kwglobal_config_subaccounts
(
    user_id            int unsigned           not null,
    rights_package     varchar(32)            not null,
    sub_rights_package varchar(32)            not null,
    subaccounts        int unsigned default 0 not null,
    primary key (user_id, rights_package, sub_rights_package)
)
    charset = utf8;

create table xb_kwglobal_keys_activate
(
    user_id         int unsigned                    not null
        primary key,
    one_time_key    varchar(32)                     not null,
    stripe_customer varchar(48)                     null,
    stripe_plans    text                            null,
    is_locked       enum ('yes', 'no') default 'no' null,
    creation_date   datetime                        not null,
    constraint activation_key
        unique (one_time_key)
)
    charset = utf8;

create table xb_kwglobal_keys_email
(
    user_id       int unsigned                                 not null
        primary key,
    one_time_key  varchar(32)                                  not null,
    email_new     varchar(255)                                 not null,
    is_locked     enum ('yes', 'no') default 'no'              null,
    creation_date datetime           default CURRENT_TIMESTAMP not null,
    constraint activation_key
        unique (one_time_key)
)
    charset = utf8;

create table xb_kwglobal_keys_password
(
    user_id       int unsigned                    not null
        primary key,
    one_time_key  varchar(32)                     not null,
    is_locked     enum ('yes', 'no') default 'no' null,
    creation_date datetime                        not null,
    constraint activation_key
        unique (one_time_key)
)
    charset = utf8;

create table xb_kwglobal_settings
(
    user_id       int unsigned                                     not null,
    settings_type enum ('single', 'all') default 'single'          not null,
    settings_key  varchar(32)                                      not null,
    settings_data text                                             not null,
    last_changed  timestamp              default CURRENT_TIMESTAMP not null,
    primary key (user_id, settings_key, settings_type)
)
    engine = MyISAM
    charset = utf8mb4;

create index UserIdType
    on xb_kwglobal_settings (user_id, settings_type);

create table xb_kwglobal_subscriptions
(
    user_id               int unsigned                           not null,
    rights_package        varchar(32)  default 'guest'           not null,
    parent_user_id        int unsigned default 0                 not null,
    parent_rights_package varchar(32)                            null,
    stripe_subscription   varchar(48)                            null,
    stripe_plan           varchar(48)                            null,
    plan                  varchar(48)                            null,
    creation_date         datetime     default CURRENT_TIMESTAMP not null,
    last_verified         datetime     default CURRENT_TIMESTAMP not null,
    primary key (user_id, rights_package)
)
    charset = utf8;

create index parents
    on xb_kwglobal_subscriptions (parent_user_id, parent_rights_package);

create table xb_kwglobal_tokens
(
    token           varchar(64)                         not null
        primary key,
    authkey         varchar(64)                         not null,
    user_id         int unsigned                        not null,
    user_token_data text                                not null,
    creation_date   datetime  default CURRENT_TIMESTAMP not null,
    last_seen       timestamp default CURRENT_TIMESTAMP not null,
    constraint authkey
        unique (authkey)
)
    charset = utf8;

create index user_id
    on xb_kwglobal_tokens (user_id, token);

create table xb_kwglobal_users
(
    user_id          int unsigned auto_increment
        primary key,
    user_status      enum ('active', 'inactive', 'banned', 'canceled', 'deleted')         default 'inactive'        not null,
    user_type        enum ('admin', 'employee', 'business', 'stripe', 'invoice', 'sales') default 'business'        not null,
    is_locked        enum ('yes', 'no')                                                   default 'no'              not null,
    user_main_portal enum ('xb')                                                          default 'xb'              not null,
    user_main_tld    varchar(2)                                                           default 'xb'              not null,
    user_email       varchar(255)                                                                                   not null,
    user_password    varchar(255)                                                                                   not null,
    user_data        text                                                                                           null,
    stripe_customer  varchar(48)                                                                                    null,
    creation_date    datetime                                                             default CURRENT_TIMESTAMP not null,
    last_changed     datetime                                                             default CURRENT_TIMESTAMP not null,
    last_verified    datetime                                                             default CURRENT_TIMESTAMP not null,
    constraint user_email
        unique (user_email)
)
    charset = utf8;

