CREATE OR REPLACE FUNCTION public.get_maintenance_status()
RETURNS jsonb
LANGUAGE sql
STABLE
SECURITY DEFINER
SET search_path = public
AS $$
  SELECT jsonb_build_object(
    'enabled', COALESCE((SELECT lower(value) = 'true' FROM public.company_settings WHERE key = 'maintenance_mode'), false),
    'message', COALESCE((SELECT value FROM public.company_settings WHERE key = 'maintenance_message'), 'The site is currently undergoing maintenance. Only administrators can sign in at this time.')
  );
$$;

GRANT EXECUTE ON FUNCTION public.get_maintenance_status() TO anon, authenticated;