-- drop dependencies
alter table if exists fleet_renewal drop constraint if exists fk_fleet_renewal_fleet_sys_pk;
-- apply changes
create table reminder (
  sys_pk                        bigint generated by default as identity not null,
  deleted_on                    timestamp,
  current_approval_level        integer default 0 not null,
  required_approval_levels      integer default 0 not null,
  next_renewal_date             date,
  last_renewal_date             date,
  amount                        float not null,
  frequency                     integer not null,
  fleet_sys_pk                  bigint,
  deleted                       boolean default false not null,
  version                       integer default 1 not null,
  created_at                    timestamp default 'now()' not null,
  modified_at                   timestamp default 'now()' not null,
  deleted_by                    varchar(255),
  approval_status               varchar(8) default 'APPROVED' not null,
  tags                          varchar[] default '{}' not null,
  comments                      jsonb default '[]' not null,
  tenant_id                     varchar(255) not null,
  type                          varchar(255) not null,
  documents                     varchar[],
  created_by                    varchar(255) not null,
  modified_by                   varchar(255) not null,
  constraint ck_reminder_approval_status check ( approval_status in ('PENDING','APPROVED','REJECTED')),
  constraint pk_reminder primary key (sys_pk)
);

create table reminder_log (
  sys_pk                        bigint generated by default as identity not null,
  deleted_on                    timestamp,
  current_approval_level        integer default 0 not null,
  required_approval_levels      integer default 0 not null,
  reminder_date                 date,
  fleet_sys_pk                  bigint,
  acted_upon                    boolean default false not null,
  amount                        float not null,
  deleted                       boolean default false not null,
  version                       integer default 1 not null,
  created_at                    timestamp default 'now()' not null,
  modified_at                   timestamp default 'now()' not null,
  deleted_by                    varchar(255),
  approval_status               varchar(8) default 'APPROVED' not null,
  tags                          varchar[] default '{}' not null,
  comments                      jsonb default '[]' not null,
  tenant_id                     varchar(255) not null,
  reminder_type                 varchar(255) not null,
  created_by                    varchar(255) not null,
  modified_by                   varchar(255) not null,
  constraint ck_reminder_log_approval_status check ( approval_status in ('PENDING','APPROVED','REJECTED')),
  constraint pk_reminder_log primary key (sys_pk)
);

-- foreign keys and indices
create index ix_reminder_fleet_sys_pk on reminder (fleet_sys_pk);
alter table reminder add constraint fk_reminder_fleet_sys_pk foreign key (fleet_sys_pk) references fleet (sys_pk) on delete restrict on update restrict;

create index ix_reminder_log_fleet_sys_pk on reminder_log (fleet_sys_pk);
alter table reminder_log add constraint fk_reminder_log_fleet_sys_pk foreign key (fleet_sys_pk) references fleet (sys_pk) on delete restrict on update restrict;

