-- Track public template likes and successful applications.
-- Safe to run multiple times after setup-all.sql / v48.

alter table public.profile_templates
  add column if not exists like_count integer not null default 0,
  add column if not exists use_count integer not null default 0;

create table if not exists public.profile_template_likes (
  template_id uuid not null references public.profile_templates(id) on delete cascade,
  user_id uuid not null references auth.users(id) on delete cascade,
  created_at timestamptz not null default now(),
  primary key (template_id, user_id)
);

update public.profile_templates as template
set like_count = (
  select count(*)::integer
  from public.profile_template_likes as template_like
  where template_like.template_id = template.id
);

alter table public.profile_template_likes enable row level security;

drop policy if exists "profile_template_likes_owner_read" on public.profile_template_likes;
create policy "profile_template_likes_owner_read" on public.profile_template_likes
  for select to authenticated using (auth.uid() = user_id);

revoke all on public.profile_template_likes from public, anon, authenticated;
grant select on public.profile_template_likes to authenticated;

revoke insert, update on public.profile_templates from public, anon, authenticated;
grant insert (user_id, name, description, data, cover_image_url, is_public, tags)
  on public.profile_templates to authenticated;
grant update (name, description, data, cover_image_url, is_public, tags)
  on public.profile_templates to authenticated;

create or replace function public.toggle_profile_template_like(target_template_id uuid)
returns table(liked boolean, like_count integer)
language plpgsql
security definer
set search_path = public, pg_temp
as $$
declare
  viewer_id uuid := auth.uid();
  template_owner uuid;
  template_is_public boolean;
begin
  if viewer_id is null then
    raise exception 'Sign in to like templates';
  end if;

  select user_id, is_public
  into template_owner, template_is_public
  from public.profile_templates
  where id = target_template_id
  for update;

  if not found or template_is_public is distinct from true then
    raise exception 'Template not found';
  end if;
  if template_owner = viewer_id then
    raise exception 'You cannot like your own template';
  end if;

  delete from public.profile_template_likes
  where template_id = target_template_id and user_id = viewer_id;

  if found then
    update public.profile_templates as target
    set like_count = greatest(target.like_count - 1, 0)
    where target.id = target_template_id;
    return query select false, t.like_count from public.profile_templates t where t.id = target_template_id;
  else
    insert into public.profile_template_likes (template_id, user_id)
    values (target_template_id, viewer_id);
    update public.profile_templates as target
    set like_count = target.like_count + 1
    where target.id = target_template_id;
    return query select true, t.like_count from public.profile_templates t where t.id = target_template_id;
  end if;
end;
$$;

create or replace function public.record_profile_template_use(target_template_id uuid)
returns integer
language plpgsql
security definer
set search_path = public, pg_temp
as $$
declare
  viewer_id uuid := auth.uid();
  updated_count integer;
begin
  if viewer_id is null then
    raise exception 'Sign in to apply templates';
  end if;

  update public.profile_templates as target
  set use_count = target.use_count + 1
  where target.id = target_template_id
    and (target.is_public = true or target.user_id = viewer_id)
  returning target.use_count into updated_count;

  if not found then
    raise exception 'Template not found';
  end if;
  return updated_count;
end;
$$;

revoke all on function public.toggle_profile_template_like(uuid) from public, anon;
grant execute on function public.toggle_profile_template_like(uuid) to authenticated;
revoke all on function public.record_profile_template_use(uuid) from public, anon;
grant execute on function public.record_profile_template_use(uuid) to authenticated;
