create table public.blog_entries (
  id bigint primary key generated always as identity,
  author_id uuid not null references auth.users (id),
  date text not null,
  title text not null,
  description text,
  slug text unique not null,
  image text,
  content jsonb default '[]'::jsonb,
  created_at timestamp with time zone default now(),
  updated_at timestamp with time zone default now()
);

create index idx_blog_entries_author_id on public.blog_entries (author_id);

alter table public.blog_entries ENABLE row LEVEL SECURITY;

create policy "Public select" on public.blog_entries for
select
  to anon using (true);

create policy "Authenticated insert" on public.blog_entries for INSERT to authenticated
with
  check (
    (
      select
        auth.uid ()
    )::uuid = author_id
  );

create policy "Author update" on public.blog_entries
for update
  to authenticated using (
    (
      select
        auth.uid ()
    )::uuid = author_id
  )
with
  check (
    (
      select
        auth.uid ()
    )::uuid = author_id
  );

create policy "Author delete" on public.blog_entries for DELETE to authenticated using (
  (
    select
      auth.uid ()
  )::uuid = author_id
);

grant select on public.blog_entries to anon;

grant INSERT, update, DELETE on public.blog_entries to authenticated;

grant USAGE, select on SEQUENCE public.blog_entries_id_seq to authenticated;
