-- Ensure uuid-ossp extension is enabled
create extension if not exists "uuid-ossp" schema extensions;

-- Function to set up initial admin
create or replace function public.setup_initial_admin()
returns void
language plpgsql
security definer
set search_path = public, extensions
as $$
begin
    -- Check if admin already exists
    if exists (
        select 1 from public.profiles 
        where user_type = 'admin'
    ) then
        return;
    end if;

    -- Grant necessary permissions
    grant all on all tables in schema public to authenticated;
    grant all on all sequences in schema public to authenticated;
    grant all on all functions in schema public to authenticated;
end;
$$;

-- Execute the function
select public.setup_initial_admin();

-- Drop the function after execution
drop function public.setup_initial_admin(); 