-- Threaded comments on general issues (mirrors notes' comments).

create table if not exists issue_comments (
  id         uuid primary key default gen_random_uuid(),
  issue_id   uuid not null references issues (id) on delete cascade,
  content    text not null default '',
  image_path text,
  author_id  uuid,
  author_email text,
  created_at timestamptz not null default now()
);
create index if not exists issue_comments_issue_id_idx on issue_comments (issue_id);

alter table issue_comments enable row level security;

drop policy if exists "authenticated full access" on issue_comments;
create policy "authenticated full access" on issue_comments
  for all to authenticated using (true) with check (true);

drop trigger if exists issue_comments_set_author on issue_comments;
create trigger issue_comments_set_author
  before insert on issue_comments
  for each row execute function set_author();

do $$ begin
  alter publication supabase_realtime add table issue_comments;
exception when duplicate_object then null;
end $$;
