create table "public"."connected_online_providers" (
    "id" bigint not null,
    "name" text not null,
    "email" text,
    "phone_number" text,
    "can_select_worker" boolean not null,
    "personal_code_required" boolean not null,
    "created_at" timestamp with time zone not null default now(),
    "updated_at" timestamp without time zone default now()
);


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

create table "public"."connected_online_services" (
    "id" bigint not null,
    "clinic_id" bigint not null,
    "sync_id" bigint not null,
    "name" text not null,
    "description" text,
    "price" double precision not null,
    "requires_payment" boolean not null,
    "duration" bigint not null,
    "neto_duration" bigint,
    "display" text,
    "price_periods" text,
    "online_hide_duration" bigint,
    "online_hide_price" bigint,
    "code" text not null,
    "has_free_codes" boolean not null,
    "created_at" timestamp with time zone not null default now(),
    "updated_at" timestamp with time zone default now()
);


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

CREATE UNIQUE INDEX connected_online_providers_id_key ON public.connected_online_providers USING btree (id);

CREATE UNIQUE INDEX connected_online_providers_pkey ON public.connected_online_providers USING btree (id);

CREATE UNIQUE INDEX connected_online_services_id_key ON public.connected_online_services USING btree (id);

CREATE UNIQUE INDEX connected_online_services_pkey ON public.connected_online_services USING btree (id);

alter table "public"."connected_online_providers" add constraint "connected_online_providers_pkey" PRIMARY KEY using index "connected_online_providers_pkey";

alter table "public"."connected_online_services" add constraint "connected_online_services_pkey" PRIMARY KEY using index "connected_online_services_pkey";

alter table "public"."connected_online_providers" add constraint "connected_online_providers_id_key" UNIQUE using index "connected_online_providers_id_key";

alter table "public"."connected_online_services" add constraint "connected_online_services_clinic_id_fkey" FOREIGN KEY (clinic_id) REFERENCES connected_online_providers(id) ON UPDATE CASCADE ON DELETE CASCADE not valid;

alter table "public"."connected_online_services" validate constraint "connected_online_services_clinic_id_fkey";

alter table "public"."connected_online_services" add constraint "connected_online_services_id_key" UNIQUE using index "connected_online_services_id_key";

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

create type "audit"."request_status" as enum ('SUCCESS', 'FAIL');

create table "audit"."request_entries" (
    "id" bigint generated by default as identity not null,
    "personal_code" bigint,
    "request_api" text not null,
    "request_api_method" text not null,
    "status" audit.request_status not null,
    "comment" text,
    "service_provider_id" bigint,
    "service_id" bigint,
    "requested_start_date" timestamp with time zone,
    "requested_end_date" timestamp with time zone,
    "created_at" timestamp with time zone not null default now()
);


alter table "audit"."request_entries" enable row level security;

CREATE UNIQUE INDEX request_entries_pkey ON audit.request_entries USING btree (id);

alter table "audit"."request_entries" add constraint "request_entries_pkey" PRIMARY KEY using index "request_entries_pkey";

grant delete on table "audit"."request_entries" to "service_role";

grant insert on table "audit"."request_entries" to "service_role";

grant references on table "audit"."request_entries" to "service_role";

grant select on table "audit"."request_entries" to "service_role";

grant trigger on table "audit"."request_entries" to "service_role";

grant truncate on table "audit"."request_entries" to "service_role";

grant update on table "audit"."request_entries" to "service_role";

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

create table "public"."connected_online_reservation" (
    "id" bigint generated by default as identity not null,
    "user_id" uuid not null,
    "booking_code" text not null,
    "service_id" bigint not null,
    "clinic_id" bigint not null,
    "service_user_id" bigint,
    "sync_user_id" bigint not null,
    "requires_payment" boolean not null,
    "comments" text,
    "start_time" timestamp with time zone not null,
    "lang" text not null,
    "discount_code" text,
    "created_at" timestamp with time zone not null default now(),
    "updated_at" timestamp with time zone default now()
);


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

CREATE UNIQUE INDEX connected_online_reservation_booking_code_key ON public.connected_online_reservation USING btree (booking_code);

CREATE UNIQUE INDEX connected_online_reservation_pkey ON public.connected_online_reservation USING btree (id);

alter table "public"."connected_online_reservation" add constraint "connected_online_reservation_pkey" PRIMARY KEY using index "connected_online_reservation_pkey";

alter table "public"."connected_online_reservation" add constraint "connected_online_reservation_booking_code_key" UNIQUE using index "connected_online_reservation_booking_code_key";

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

alter table "public"."connected_online_reservation" validate constraint "connected_online_reservation_user_id_fkey";

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

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

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

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

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

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

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

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


CREATE TRIGGER connected_online_providers_change_record_timestamps AFTER INSERT OR UPDATE ON public.connected_online_providers FOR EACH ROW EXECUTE FUNCTION trigger_set_timestamps();

CREATE TRIGGER connected_online_services_change_record_timestamps AFTER INSERT OR UPDATE ON public.connected_online_services FOR EACH ROW EXECUTE FUNCTION trigger_set_timestamps();

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


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

create policy "authenticated_select"
on "public"."connected_online_providers"
as permissive
for select
to authenticated
using (true);

create policy "authenticated_select"
on "public"."connected_online_services"
as permissive
for select
to authenticated
using (true);


create policy "own_all"
on "public"."connected_online_reservation"
as permissive
for all
to authenticated
using ((( SELECT auth.uid() AS uid) = user_id));