-- Keep the public like-count RPC consistent with both privacy columns.

update public.profiles
set
  hide_bookmarks = coalesce(hide_bookmarks, false) or coalesce(hide_likes, false),
  hide_likes = coalesce(hide_bookmarks, false) or coalesce(hide_likes, false)
where hide_bookmarks is distinct from (coalesce(hide_bookmarks, false) or coalesce(hide_likes, false))
   or hide_likes is distinct from (coalesce(hide_bookmarks, false) or coalesce(hide_likes, false));

create or replace function public.get_profile_like_state(target_username text)
returns table(liked boolean, likes integer)
language plpgsql
security definer
set search_path = public
as $$
declare
  target_id uuid;
  current_user_id uuid;
begin
  select id into target_id
  from public.profiles
  where username = lower(trim(target_username))
     or alias = lower(trim(target_username))
  limit 1;

  if target_id is null then
    return query select false, 0;
    return;
  end if;

  current_user_id := auth.uid();
  return query
  select
    case when current_user_id is null then false else exists (
      select 1 from public.profile_likes l
      where l.profile_id = target_id and l.liker_id = current_user_id
    ) end,
    coalesce((
      select case when coalesce(p.hide_bookmarks or p.hide_likes, false) then 0 else p.like_count end
      from public.profiles p where p.id = target_id
    ), 0)::int;
end;
$$;

create or replace function public.toggle_profile_like(target_username text)
returns table(liked boolean, likes integer)
language plpgsql
security definer
set search_path = public
as $$
declare
  target_id uuid;
  current_user_id uuid;
  affected int;
begin
  current_user_id := auth.uid();
  if current_user_id is null then raise exception 'not_authenticated'; end if;
  if not exists (select 1 from public.profiles p where p.id = current_user_id) then
    raise exception 'profile_not_claimed';
  end if;

  select id into target_id
  from public.profiles
  where username = lower(trim(target_username))
     or alias = lower(trim(target_username))
  limit 1;
  if target_id is null then raise exception 'profile_not_found'; end if;

  if exists (select 1 from public.profile_likes where profile_id = target_id and liker_id = current_user_id) then
    delete from public.profile_likes where profile_id = target_id and liker_id = current_user_id;
    update public.profiles set like_count = greatest(coalesce(like_count, 0) - 1, 0) where id = target_id;
    return query select false, coalesce((
      select case when coalesce(p.hide_bookmarks or p.hide_likes, false) then 0 else p.like_count end
      from public.profiles p where p.id = target_id
    ), 0)::int;
    return;
  end if;

  insert into public.profile_likes(profile_id, liker_id)
  values (target_id, current_user_id) on conflict do nothing;
  get diagnostics affected = row_count;
  if affected > 0 then
    update public.profiles set like_count = coalesce(like_count, 0) + 1 where id = target_id;
  end if;

  return query select true, coalesce((
    select case when coalesce(p.hide_bookmarks or p.hide_likes, false) then 0 else p.like_count end
    from public.profiles p where p.id = target_id
  ), 0)::int;
end;
$$;

revoke all on function public.get_profile_like_state(text) from public;
grant execute on function public.get_profile_like_state(text) to anon, authenticated;
revoke all on function public.toggle_profile_like(text) from public, anon;
grant execute on function public.toggle_profile_like(text) to authenticated;
