create type "public"."analysis_order_status" as enum ('QUEUED', 'ON_HOLD', 'PROCESSING', 'COMPLETED', 'REJECTED', 'CANCELLED');

create table "public"."analysis_orders" (
    "id" uuid not null,
    "analysis_element_ids" bigint[],
    "analysis_ids" bigint[],
    "user_id" uuid not null,
    "status" public.analysis_order_status not null,
    "created_at" timestamp with time zone not null default now()
);

alter table "public"."analysis_orders" enable row level security;

CREATE UNIQUE INDEX analysis_orders_pkey ON public.analysis_orders USING btree (id);

alter table "public"."analysis_orders" add constraint "analysis_orders_pkey" PRIMARY KEY using index "analysis_orders_pkey";

alter table "public"."analysis_orders" add constraint "analysis_orders_id_fkey" FOREIGN KEY (id) REFERENCES auth.users(id) ON UPDATE CASCADE ON DELETE CASCADE not valid;

alter table "public"."analysis_orders" validate constraint "analysis_orders_id_fkey";

grant delete on table "public"."analysis_orders" to "authenticated";

grant insert on table "public"."analysis_orders" to "authenticated";

grant references on table "public"."analysis_orders" to "authenticated";

grant select on table "public"."analysis_orders" to "authenticated";

grant trigger on table "public"."analysis_orders" to "authenticated";

grant truncate on table "public"."analysis_orders" to "authenticated";

grant update on table "public"."analysis_orders" to "authenticated";

grant delete on table "public"."analysis_orders" to "service_role";

grant insert on table "public"."analysis_orders" to "service_role";

grant references on table "public"."analysis_orders" to "service_role";

grant select on table "public"."analysis_orders" to "service_role";

grant trigger on table "public"."analysis_orders" to "service_role";

grant truncate on table "public"."analysis_orders" to "service_role";

grant update on table "public"."analysis_orders" to "service_role";

create policy "analysis_all"
on "public"."analysis_orders"
as permissive
for all
to authenticated, service_role
using (true);

create policy "analysis_select"
on "public"."analyses"
as permissive
for select
to public
using (true);

create policy "analysis_elements_select"
on "public"."analysis_elements"
as permissive
for select
to public
using (true);

create policy "analysis_groups_select"
on "public"."analysis_groups"
as permissive
for select
to public
using (true);

-- Drop previously created unnecessary indices
drop index if exists "public"."analysis_elements_original_id_key";
drop index if exists "public"."analysis_original_id_key";

-- Remove nullable from previously added fields
alter table "public"."analyses" alter column "order" set not null;
alter table "public"."analysis_elements" alter column "analysis_id_oid" set not null;
alter table "public"."analysis_elements" alter column "order" set not null;
alter table "public"."analysis_groups" alter column "name" set not null;
alter table "public"."analysis_groups" alter column "order" set not null;
alter table "public"."analysis_elements" alter column "tehik_loinc_name" set not null;
alter table "public"."analysis_elements" alter column "tehik_short_loinc" set not null;


