drop function if exists public.add_invitations_to_account(text, public.invitation[]);

drop type if exists public.invitation;

create type public.invitation as (
  email text,
  role text,
  personal_code char(11)
);

create or replace function public.add_invitations_to_account (
    account_slug text,
    invitations public.invitation[]
) returns public.invitations[]
    set search_path = ''
as $$
declare
    new_invitation public.invitations;
    all_invitations public.invitations[] := array[]::public.invitations[];
    invite_token text;
    invite public.invitation;
begin
    foreach invite in array invitations loop
        invite_token := extensions.uuid_generate_v4();

        insert into public.invitations (
            email,
            account_id,
            invited_by,
            role,
            invite_token,
            personal_code
        )
        values (
            invite.email,
            (select id from public.accounts where slug = account_slug),
            auth.uid(),
            invite.role,
            invite_token,
            invite.personal_code
        )
        returning * into new_invitation;

        all_invitations := array_append(all_invitations, new_invitation);
    end loop;

    return all_invitations;
end;
$$ language plpgsql;

grant execute on function public.add_invitations_to_account(text, public.invitation[]) to authenticated;

drop function if exists public.get_account_invitations(text);

create function public.get_account_invitations (account_slug text)
returns table (
  id integer,
  email varchar(255),
  account_id uuid,
  invited_by uuid,
  role varchar(50),
  created_at timestamptz,
  updated_at timestamptz,
  expires_at timestamptz,
  personal_code text,
  inviter_name varchar,
  inviter_email varchar
)
set search_path = ''
as $$
begin
  return query
    select
      invitation.id,
      invitation.email,
      invitation.account_id,
      invitation.invited_by,
      invitation.role,
      invitation.created_at,
      invitation.updated_at,
      invitation.expires_at,
      invitation.personal_code,
      account.name,
      account.email
    from
      public.invitations as invitation
      join public.accounts as account on invitation.account_id = account.id
    where
      account.slug = account_slug;
end;
$$ language plpgsql;

grant execute on function public.get_account_invitations(text) to authenticated, service_role;


drop function if exists public.get_account_members(text);

-- Functions "public.get_account_members"
-- Function to get the members of an account by the account slug
create
    or replace function public.get_account_members (account_slug text) returns table (
                                                                                         id uuid,
                                                                                         user_id uuid,
                                                                                         account_id uuid,
                                                                                         role varchar(50),
                                                                                         role_hierarchy_level int,
                                                                                         primary_owner_user_id uuid,
                                                                                         name varchar,
                                                                                         email varchar,
                                                                                         personal_code text,
                                                                                         picture_url varchar,
                                                                                         created_at timestamptz,
                                                                                         updated_at timestamptz
                                                                                     ) language plpgsql
    set
        search_path = '' as $$
begin
    return QUERY
        select
            acc.id,
            am.user_id,
            am.account_id,
            am.account_role,
            r.hierarchy_level,
            a.primary_owner_user_id,
            acc.name,
            acc.email,
            acc.personal_code,
            acc.picture_url,
            am.created_at,
            am.updated_at
        from
            public.accounts_memberships am
                join public.accounts a on a.id = am.account_id
                join public.accounts acc on acc.id = am.user_id
                join public.roles r on r.name = am.account_role
        where
                a.slug = account_slug;

end;

$$;

grant
    execute on function public.get_account_members (text) to authenticated,
    service_role;
