create type entry_type as enum ('SPENDING', 'INCOME');
create type recurring_frequency as enum ('DAILY', 'WEEKLY', 'BIWEEKLY', 'MONTHLY', 'QUARTERLY', 'YEARLY');
create type recurring_end_condition as enum ('NEVER', 'AFTER_COUNT', 'BY_DATE');

alter table spendings rename to entries;

alter index idx_spendings_group_occurred rename to idx_entries_group_occurred;
alter index idx_spendings_receipt rename to idx_entries_receipt;

alter table entries
    add column entry_type entry_type not null default 'SPENDING',
    add column is_recurring boolean not null default false,
    add column frequency recurring_frequency,
    add column interval_count integer not null default 1,
    add column end_condition recurring_end_condition,
    add column end_count integer,
    add column end_date date,
    add column next_run_at date,
    add column last_executed_at date;

alter table spending_tags rename to entry_tags;
alter table entry_tags rename column spending_id to entry_id;

alter table entry_tags rename constraint spending_tags_pkey to entry_tags_pkey;

alter index spending_tags_spending_idx rename to entry_tags_entry_idx;
alter index spending_tags_tag_idx rename to entry_tags_tag_idx;
