alter table "public"."accounts" add column "last_name" text;

alter table "public"."accounts" add column "personal_code" text;

alter table "public"."accounts" add column "city" text;

alter table "public"."accounts" add column "has_consent_personal_data" boolean;

alter table "public"."accounts" add column "phone" text;

set check_function_bodies = off;

CREATE OR REPLACE FUNCTION public.update_account(p_name character varying, p_last_name text, p_personal_code text, p_phone text, p_city text, p_has_consent_personal_data boolean, p_uid uuid)
 RETURNS void
 LANGUAGE plpgsql
AS $function$begin
  update public.accounts
  set    name                      = coalesce(p_name, name),
         last_name                 = coalesce(p_last_name, last_name),
         personal_code             = coalesce(p_personal_code, personal_code),
         phone                     = coalesce(p_phone, phone),
         city                      = coalesce(p_city, city),
         has_consent_personal_data = coalesce(p_has_consent_personal_data,
                                              has_consent_personal_data)
  where  id = p_uid; 
end;$function$
;

grant
execute on function public.update_account(p_name character varying, p_last_name text, p_personal_code text, p_phone text, p_city text, p_has_consent_personal_data boolean, p_uid uuid) to authenticated,
service_role;

CREATE OR REPLACE FUNCTION public.has_personal_code(account_id uuid)
 RETURNS boolean
 LANGUAGE plpgsql
AS $function$BEGIN
  RETURN EXISTS (
    SELECT 1
    FROM public.accounts
    WHERE id = account_id
      AND personal_code IS NOT NULL
      AND personal_code <> ''
  );
END;$function$
;

grant
execute on function public.has_personal_code(account_id uuid) to authenticated,
service_role;

create table "public"."account_params" (
    "recorded_at" timestamp with time zone not null default now(),
    "id" uuid not null default gen_random_uuid(),
    "account_id" uuid not null default auth.uid(),
    "weight" integer,
    "height" integer
);

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

CREATE UNIQUE INDEX account_params_pkey ON public.account_params USING btree (id);

alter table "public"."account_params" add constraint "account_params_pkey" PRIMARY KEY using index "account_params_pkey";

grant delete on table "public"."account_params" to "anon";

grant insert on table "public"."account_params" to "anon";

grant references on table "public"."account_params" to "anon";

grant select on table "public"."account_params" to "anon";

grant trigger on table "public"."account_params" to "anon";

grant truncate on table "public"."account_params" to "anon";

grant update on table "public"."account_params" to "anon";

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

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

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

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

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

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

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

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

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

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

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

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

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

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

-- Everyone can read their own rows
create policy "users can read their params"
on public.account_params
for select
to authenticated
using ( account_id = auth.uid() );

-- Everyone can insert rows for themselves
create policy "users can insert their params"
on public.account_params
for insert
to authenticated
with check ( account_id = auth.uid() );