create table "public"."analysis_response_elements" (
    "id" bigint generated by default as identity not null,
    "analysis_response_id" bigint not null,
    "analysis_element_original_id" text not null,
    "unit" text,
    "response_value" jsonb not null,
    "response_time" timestamp with time zone not null,
    "norm_upper" double precision,
    "norm_upper_included" boolean,
    "norm_lower" double precision,
    "norm_lower_included" boolean,
    "norm_status" smallint,
    "original_response_element" jsonb not null,
    "created_at" timestamp with time zone not null default now(),
    "updated_at" timestamp with time zone default now()
);


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

create table "public"."analysis_responses" (
    "id" bigint generated by default as identity not null,
    "analysis_order_id" bigint not null,
    "order_number" text not null,
    "order_status" analysis_order_status not null,
    "user_id" uuid not null,
    "created_at" timestamp with time zone not null default now(),
    "updated_at" timestamp with time zone default now()
);

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

CREATE UNIQUE INDEX analysis_response_element_pkey ON public.analysis_response_elements USING btree (id);

CREATE UNIQUE INDEX analysis_response_pkey ON public.analysis_responses USING btree (id);

CREATE UNIQUE INDEX analysis_responses_order_number_key ON public.analysis_responses USING btree (order_number);

alter table "public"."analysis_response_elements" add constraint "analysis_response_element_pkey" PRIMARY KEY using index "analysis_response_element_pkey";

alter table "public"."analysis_responses" add constraint "analysis_response_pkey" PRIMARY KEY using index "analysis_response_pkey";

alter table "public"."analysis_response_elements" add constraint "analysis_response_element_analysis_response_id_fkey" FOREIGN KEY (analysis_response_id) REFERENCES analysis_responses(id) ON UPDATE CASCADE ON DELETE CASCADE not valid;

alter table "public"."analysis_response_elements" validate constraint "analysis_response_element_analysis_response_id_fkey";

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

alter table "public"."analysis_responses" validate constraint "analysis_response_user_id_fkey";

alter table "public"."analysis_responses" add constraint "analysis_responses_order_number_key" UNIQUE using index "analysis_responses_order_number_key";

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

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

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

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

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

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

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

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

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

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

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

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

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

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

create policy "select_own"
on "public"."analysis_response_elements"
as permissive
for select
to authenticated
using ((( SELECT auth.uid() AS uid) IN ( SELECT analysis_responses.user_id
   FROM analysis_responses
  WHERE (analysis_responses.id = analysis_response_elements.analysis_response_id))));


create policy "service_role_all"
on "public"."analysis_response_elements"
as permissive
for all
to service_role
using (true);


create policy "select_own"
on "public"."analysis_responses"
as permissive
for select
to authenticated
using ((( SELECT auth.uid() AS uid) = user_id));


create policy "service_role_all"
on "public"."analysis_responses"
as permissive
for all
to service_role
using (true);

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


alter table "public"."analysis_orders" add constraint "analysis_orders_user_id_fkey" FOREIGN KEY (user_id) REFERENCES auth.users(id) ON UPDATE CASCADE ON DELETE CASCADE not valid;
alter table "public"."analysis_orders" drop constraint "analysis_orders_id_fkey";
alter table "public"."analysis_orders" drop column "id";
alter table "public"."analysis_orders" add "id" bigint generated by default as identity not null;
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_responses" add constraint "analysis_responses_analysis_order_id_fkey" FOREIGN KEY (analysis_order_id) REFERENCES analysis_orders(id) ON UPDATE CASCADE ON DELETE CASCADE not valid;
alter table "public"."analysis_responses" validate constraint "analysis_responses_analysis_order_id_fkey";
