import { notFound } from 'next/navigation';

import { getSupabaseServerClient } from '@kit/supabase/server-client';

import { isDoctor } from './is-doctor';

/**
 * @name doctorAction
 * @description Wrap a server action to ensure the user is a doctor.
 * @param fn
 */
export function doctorAction<Args, Response>(
  fn: (params: Args) => Promise<Response>,
) {
  return async (params: Args): Promise<Response> => {
    const isUserDoctor = await isDoctor(getSupabaseServerClient());

    if (!isUserDoctor) {
      notFound();
    }

    return await fn(params);
  };
}
