create table public.challenges
(
    id         bigint generated by default as identity,
    answers    jsonb        not null,
    details    jsonb        not null,
    author     uuid         not null,
    problem    int8         not null,
    status     varchar(256) not null,
    created_at timestamp with time zone null default now(),
    constraint challenges_pkey primary key (id),
    constraint challenges_author_fkey foreign key (author) references auth.users (id),
    constraint challenges_problem_fkey foreign key (problem) references public.problems (id)
) tablespace pg_default;

alter table public.challenges enable row level security;

create
policy "Enable insert for everyone" on "public"."challenges"
as permissive for insert
to public
with check (true);

create
policy "Enable read access for users' own items" on "public"."challenges"
as permissive for
select
    to public
    using (author = auth.uid());

create
policy "Enable update access for users' own items" on "public"."challenges"
as permissive for
update
    to public
    using (author = auth.uid() and status = 'in-progress')
with check (author = auth.uid())