-- +goose Up
-- +goose StatementBegin
create table users (
  id text primary key,
  username text not null,
  biography text
);

create table bots (
  id text primary key,
  username text not null,
  avatar text,
  overview text,
  description text,
  is_slash boolean not null,
  install_context text check (install_context in ('guild', 'user')) not null,
  guild_count int,
  install_count int,
  imported_from text,
  prefix text,
  created_at timestamp with time zone default now(),
  updated_at timestamp with time zone default now(),
  status text check (
    status in ('deleted', 'pending', 'rejected', 'approved')
  ) default 'pending',
  main_owner_id text references users (id)
);

create table bot_co_owners (
  bot_id text references bots (id),
  user_id text references users (id),
  primary key (bot_id, user_id)
);

create table votes (
  user_id text references users (id),
  bot_id text references bots (id),
  voted_at timestamp with time zone default now(),
  primary key (user_id, bot_id, voted_at)
);

create
or replace function can_vote_again (user_id text, bot_id text) returns boolean as $$
BEGIN
    RETURN NOT EXISTS (
        SELECT 1 FROM votes
        WHERE user_id = $1 AND bot_id = $2 AND voted_at > now() - interval '12 hours'
    );
END;
$$ language plpgsql;
-- +goose StatementEnd


-- +goose Down
-- +goose StatementBegin
drop function if exists can_vote_again;

drop table if exists votes;
drop table if exists bot_co_owners;
drop table if exists bots;
drop table if exists users;
-- +goose StatementEnd
